Wednesday, September 21, 2011

shutdown writing on a socket

Shutting down Writing to a Socket

The following code shows how to indicate that no further writes will be performed upon the local socket:
Example

int z;
int s; /* Socket */
z = shutdown(s, SHUT_WR);
if ( z == -1 ) {
   perror("shutdown()");
}

Shutting down the writing end of a socket solves a number of thorny problems. They are:
  • Flushes out the kernel buffers that contain any pending data to be sent. Data is buffered by the kernel networking software to improve performance. 
  • Sends an end-of-file indication to the remote socket. This tells the remote reading process that no more data will be sent to it on this socket. 
  • Leaves the partially shutdown socket open for reading. This makes it possible to receive confirmation messages after the end-of-file indication has been sent on the socket. 
  • Disregards the number of open references on the socket. Only the last close(2) on a socket will cause an end-of-file indication to be sent.
The last point requires a bit of explanation, which is provided in the my post "Dealing with Duplicated Sockets"

No comments:

Post a Comment