Search code examples
clinuxsocketswinsocksend

C LINUX\WINSOCK- check FIN packet in Send


I am trying to understand how to avoid the following scenario:

  1. A client kills its process. A FIN message is sent to server.
  2. Server receives the FIN message from the client.
  3. A milisecond after the server receives the FIN message, the server uses the send message, and sends information to the client.

Using the C API- can the server know what packets are acknowledged by the client?

If so- what are the commands in Linux\Winsock?


Solution

  • This question comes up periodically. Short answer: The TCP layer in the OS intentionally does not pass up "acks" (acknowledgement of receipt) to the application layer. And if it did, it would be the rope you would hang yourself by. While TCP is considered "reliable", it doesn't actually have a way to indicate if the application code above it has actually processed the received bytes.

    You mentioned "packets", but that is a very ambiguous term. Your socket application may have the notion of "messages" (not packets), but TCP does not have the concept of a packet or even a message. It sends byte "streams" that originate from your application code. TCP segmentation, IP fragmentation, and other factors will split your message up into multiple packets on the wire. And TCP has no knowledge of what IP packets make up the entire application message. (Common socket fallacy - many developers erroneously believe a "send" corresponds to an identically sized "recv" on the other side).

    So the only code that can acknowledge success receipt of a message is the socket application itself. In other words, your client/server protocol should have its own system of acknowledgements.