   Reading/Writting binary files and sockets
  Hey there. Making a basic basic console applicaiion got all the commands working and directory scaning and such. Im having a problem with binary files tho. the only thing that sends correctly in binary format is a txt file. heres the code i have./**@fn void get ( fs::path& currentDir ) @param currentDir [in] current directory client or server is browsing@param socket [in] socket to receive data across@param transferType [in] how to receive the file (binary or text)@brief receives a file and gives it the same name */void getFile ( fs::path& currentDir, Socket& socket, int transferType  ){string fileCheck;socket.Receive(fileCheck);if ( fileCheck != FILE_ERROR ){//Get File Sizestring fileSize;socket.Receive( fileSize );cout << "File Size = " << fileSize << " kb" << endl;//Get File Namestring fileName;socket.Receive( fileName );cout << "File Name = " << fileName << endl;istringstream iss;iss.str( fileSize );long fsize = 0;iss >> fsize;int bytesRecieved;ofstream receiveFile;fs::path location = currentDir /= fileName;//Open a new fileif(transferType == binary)receiveFile.open (location.string().c_str(), ios::out | ios::binary );elsereceiveFile.open (location.string().c_str() );receiveFile.flush();receiveFile.clear();//Get the filewhile( fsize > 0 ){string buffer;bytesRecieved = socket.Receive( buffer );if (transferType == binary)receiveFile.write( buffer.c_str(), bytesRecieved) ;elsereceiveFile << buffer << '\0';fsize -= (long)bytesRecieved;}cout << "Fully Received File" << endl;//Close the filereceiveFile.close();}elsecout << FILE_ERROR << endl;}/**@fn void put ( fs::path const & currentDir, string const & fileName, Socket const& socket )    @param currentDir [in] current directory client or server is browsing@param fileName [in] which file to send@param socket [in] socket to send data across@param transferType [in] how to send the file (binary or text)@brief send a file across a socket */void putFile ( fs::path const& currentDir, string const& fileName, Socket& socket, int transferType ){//Get File Locationfs::path fileLocation( currentDir );fileLocation /= fileName;if ( fs::exists( fileLocation ) ){//Send Cmd to prepare server/client to receiveostringstream oss;oss << put;socket.Send( "FILE_OK" );oss.str("");int bytesSent;ifstream sendFile;if(transferType == binary)sendFile.open(fileLocation.string().c_str(), ios::in | ios::binary);elsesendFile.open(fileLocation.string().c_str() );sendFile.seekg(0,ifstream::end);long fileSize = sendFile.tellg();sendFile.seekg(0);oss << fileSize + 1;//Send File Sizesocket.Send( oss.str() );//Send File Namesocket.Send( fileLocation.leaf() );//Send Filewhile( fileSize > 0 )    {try{if ( transferType == binary ){char binaryBuffer[SEND_BUFFER_SIZE];if (fileSize > SEND_BUFFER_SIZE)sendFile.read( binaryBuffer, SEND_BUFFER_SIZE );else{sendFile.read( binaryBuffer, fileSize );binaryBuffer[fileSize] = '\0';}bytesSent = socket.Send( binaryBuffer );fileSize -= bytesSent;}else{string textBuffer;getline( sendFile, textBuffer, '\n' );bytesSent = socket.Send( textBuffer );fileSize -= bytesSent;}}catch( info5030_05f::SocketException::Exception &se){cout << se.what();break;}}//Close FilesendFile.close();}elsesocket.Send(FILE_ERROR);}Anyone see any errors there?and i have a quick question with sockets, iv tried building a socket wrapper and im wonding if sockets are like queues at all.say i send three things in a row. then i receive 1, are the other 2 messages stored in a buffer always waiting there for me to grab? becuase currently if i dont add a little delay in the send (sleep(50) the sockets will hang forever on a recv. I'll post code on that too if anyone is willing to help on that situation. Thanks guys   
  Any body at all?   ;  Don't do this:Quote:string buffer;bytesRecieved = socket.Receive( buffer );You're not supposed to put binary data into a string. For starters, it will be cut off at the first null character.Read into a plain char array instead (like binaryBuffer), and provide the size of this char array as the second parameter to 'Receive'. Also be sure to specify the second parameter to 'Send'.AFAIK, you can't write directly into a string's buffer like that anyway. You'll probably crash the program.   