C Sockets: write() followed by close() results in incomplete data transfer -


I am trying to write a basic file server that takes the file name from the client and responds by sending data from TCP Customer's For the most part I have a working client and server application, but I am seeing some strange behaviors, consider the following

  (num_read = read (file_fd, file_buffer, sizeof (file_buffer) ))) & Gt; 0) {if (num_read! = Write (conn_fd, article_buffer, num_read)} {mirror ("write"); Goto out; }} OUT: OFF (file_fd); Close (sub_fd);   

file_fd is a file descriptor for the file being sent to the network, conn_fd to connect to a one File descriptor () ed TCP socket.

This works for small files, but when my files become large (megabyte +) it seems that some non-stability will fail to transfer the data at the end of the file.

After writing, I have a doubt about the immediate () statement, so I stopped 1 second sleep () () before the statement and my client Has successfully acquired all the data.

What sleep () / code> on the server side? A successful "write" on the socket means the data has been successfully sent to the co-worker

P> If you are on a unix derivative, you can do a "man 7 socket" and check SO_LINGER as a possible solution.

EDIT: EZ comment (due to thanksgiving), I read Stephen again in "Unix Network Programming" to ensure the delivery of all the data for an equivalent. He says the following (Volume 1 of second edition, page 189):

... We see that when we close the connection (closed or closed) and whether it is SO_LINGER Depending on the socket option is set, the return may occur at three different times.

  • Unless the ACK of our FIAC has been received (Figure 7.7), or
  • As long as we do not receive their FIN of Peer (Figure 7.8)

    , and their comments, other than "application level acceptance" Indicates the closed () , followed by a reading () a zero return code (i.e., notice that the socket was closed ), The only way to ensure that the client application has received the data.

    However, it is only important that the data is successfully delivered (and accepted) peer's computer, then SO_LINGER will be sufficient.

  • Comments

    Popular posts from this blog

    php - PDO bindParam() fatal error -

    logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

    java - Why my included JSP file won't get processed correctly? -