mardi 21 juillet 2015

Proper use of std::string to send binary data through a socket

I'm trying to send files splitted in chunks over a TCP Berkley ocket but i'm gettin unexpected behaviors.

std::ifstream inFile (path.c_str(),std::ios::in|std::ios::binary|std::ios::ate); 
std::streampos _size;
_size = inFile.tellg();
char * _buffer = new char [MAXLINE];
int    _chunk = 0;
inFile.seekg (0, std::ios::beg);

msg << "ACK" << " " << "START" << " " << message[1];
connTSock->send_message(msg.str());

while(inFile.read(_buffer , MAXLINE))
{
   CLEAR_MSG(msg);
   msg << "ACK" << " " << "CHUNK" << " " << _chunk << " " << _buffer;
   _chunk++;
   connTSock->send_message(msg.str());
}

In few words I am getting a loss of data , the client opens an output stream and writes in it , but i.e. of 11 kb of the original file i get only 7kb on the other side. I guess that the problem could be related to the fact that I'm pushing the _buffer in a std::stringstream and then converting it in a std::string. Am I right? How could I fix this problem?

Aucun commentaire:

Enregistrer un commentaire