Skip to main content



      Home
Home » Eclipse Projects » Paho » Sending a file via MQTT C
Sending a file via MQTT C [message #1326478] Thu, 01 May 2014 15:30 Go to next message
Eclipse UserFriend
Hello everyone,

I am trying to send a file (rather than a string; see the example) via MQTT to my broker, however I'm not sure if I'm going about this in the right way:

long GetFileSize(std::string absoluteFile)
{
FILE * myFile;
long size = 0;

myFile = std::fopen(absoluteFile.c_str(), "rb");
if(myFile == NULL)
{
std::cout << "Error opening file/file not found." << std::endl;
}
else
{
std::fseek(myFile, 0, SEEK_END);
size = ftell(myFile);
fclose(myFile);
}

return size;
}

// Add file to the payload.
FILE * pFile = fopen(myFile.c_str(), "rb");
pubmsg.payload = pFile;
pubmsg.payloadlen = GetFileSize(myFile); // Assuming I shouldn't be using sizeof(pFile) here.
pubmsg.qos = g_QOS;
pubmsg.retained = 0;

Is this an acceptable implementation of sending a file using synchronous MQTT?

The broker receives a byte array of the file in unreadable ASCII characters, so I will have to figure out how to interpret this data on the other side as well. Any tips on how to reconstruct this file on the other end for other subscribers to the topic?

Thanks,
Brandon
Re: Sending a file via MQTT C [message #1327605 is a reply to message #1326478] Fri, 02 May 2014 04:39 Go to previous message
Eclipse UserFriend
Hi Brandon,

pFile is a file handle, not the contents of a file. You need to read the contents of the file into a buffer (with fread or similar http://pubs.opengroup.org/onlinepubs/009695399/functions/fread.html) and set that buffer to be the payload.

Ian
Previous Topic:Commercial use of Mihini and MQTT/Mosquitto/Paho
Next Topic:How to compile C++ libraries in Visual Studio
Goto Forum:
  


Current Time: Wed May 14 05:35:08 EDT 2025

Powered by FUDForum. Page generated in 0.03519 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top