简体   繁体   中英

C++ How to properly receive a file in Winsock?

I'm working on a CHAT Program in C++. And stuck upon a problem of sending/receiving file. My server side is Python that runs in background of Flask App accepting TCP Connections and forwarding incoming/outgoing data to chat clients. It has no problem and works great. My problem is on the client receiving. I added this feature of Sending and receiving file. While send goes out fine with no error (Tested and it works). Receiving on the client side has gave me a headache.

I'm using the following to receive the file whether it is binary or Text.

// filebuf is defined as
// char filebuf[BUFFER];
// BUFFER is 1024.
void CHAT::recvFile()
{
 int fsize;
 std::ofstream recvfile(filename, std::ios::app, std::ios::binary);
 while ((fsize = recv(sockfd, filebuf, sizeof(filebuf), 0)) > 0)
 {
     recvfile.write(filebuf, sizeof(filebuf));
 }
 recvfile.close();
 respond("Received file.\n");
}

This works but the data is not written in the file fully. For Example; If I send a 144 kb File it will be saved on the other side as 142 kb. And the program just stops there.

Final Question; How do I receive a large/small file in C++?

EDIT : After @Algirdas Preidžius Response, I retested it. Here are some screenshots of the file before and after sent.

This is before it's sent.

https://www.upload.ee/image/10164543/ook.PNG

This is after it's sent. https://www.upload.ee/image/10164544/wsa.PNG

There are 3 ways you can handle this situation.

  • Message delimiters/line terminator
  • Inserting the size of the message before the message
  • Use non-blocking IO in conjunction with select

Here is a quick example of parsing the incoming message looking for a line terminator. For example:

int fsize;
std::string filebuf( '0', 1024 );

while ( ( fsize = recv( sockfd, filebuf.data( ), filebuf.size( ), 0 ) ) > 0 )
{
     if( std::size_t index{ filebuf.find( "#end", 0 ) }; 
          index != std::string::npos )
     {
         // The line terminator was found so don't call the
         // the receive function again as there should be nothing
         // remaining in the stream to read.
     }
     /*Write the data to your file.*/
     filebuf.clear( );
}
respond("Received file.\n");

Note that this is a quick untested example showing you how you might parse an incoming message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM