Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Sending a file via MQTT C
Sending a file via MQTT C [message #1326478] Thu, 01 May 2014 19:30 Go to next message
Brandon Palm is currently offline Brandon PalmFriend
Messages: 1
Registered: May 2014
Junior Member
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;
std::fseek(myFile, 0, SEEK_END);
size = ftell(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?

Re: Sending a file via MQTT C [message #1327605 is a reply to message #1326478] Fri, 02 May 2014 08:39 Go to previous message
Ian Craggs is currently offline Ian CraggsFriend
Messages: 76
Registered: March 2013
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 and set that buffer to be the payload.

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

Current Time: Sat Dec 05 00:45:52 GMT 2020

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

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

Back to the top