I have written a program that communicates with a server via sockets. To make my life easier I created a 'socket' class which takes care of all the low-level socket stuff. One of the methods is called 'send' and you'd never guess, but it handles the sending of data. Because I will sometimes be using it for large packets, I have implemeted a way of making sure all data is sent before it returns. Right now it accepts a string, sends what it can, and then loops until it has sent the rest.
Unfortunately I can no longer use the handy std::string class for this method. Some of my data must be sent as an 8 byte character array, which is picked up as two 4-byte __int32 values at the other end. When converted to a string, this data is somehow corrupted and not read properly by the server. It must be sent as a char type and must have measures in place to ensure all data is sent.
Right now, this is what it looks like..
As you can see, right now it makes use of the substr() method of the string class. I have scoured google for the past hour looking for a way of doing the same thing with a character array, but havent found anythingCode://Sends a string of data void SockWrap::sSend(std::string data){ //Initialise variables int bytes_sent = 0; int total = 0; //Get length of data in bytes.. int len = data.length(); //Loop until all data has been sent.. while (total < len) { //Send data... //If statement checks for errors. -1 is returned by send() if error occurs.. if ((bytes_sent+=send(this->sockfd, data, data.length(), 0)) == -1){ std::cout << "Error sending data!" << std::endl; system("PAUSE"); exit(1); } total += bytes_sent; //Removes data just sent from the data string.. data = data.substr(bytes_sent, data.length()-bytes_sent); } }
Anyone have any ideas how I might solve this?



LinkBack URL
About LinkBacks




CornedBee