=========================== EOF handling between client and server =========================== -Ian! D. Allen - idallen@idallen.ca Make sure your client and server handle EOF correctly. A typical client has two processes (or perhaps two threads) - one reading Standard Input (often the keyboard) and writing the connected server socket, the other reading the connected server socket and writing to Standard Output (usually the screen). Let's call the first one the client Stdin process and the second one the client Stdout process. On getting EOF from the keyboard (usually triggerd by ^D), the client Stdin process should use shutdown(fd,1) on the connected server socket and then exit. This shutdown will close only the client's writing (not reading) half of the socket in both client processes. (Don't close the reading half yet! Close only the client writing direction of the socket; make sure the argument to shutdown is 1 or SHUT_WR.) The client Stdout process will continue to run, still reading from non-closed reading half of the server socket. (Only the writing direction of the socket should be shut down using shutdown(fd,1).) After the client Stdin process does shutdown(fd,1), the server will see the EOF on its matching reading half of the client socket, and will fully close() the socket at its end. Before the server closes the socket, it might wish to write a "goodbye" message to the client. After the server closes the socket, the client Stdout process will now see EOF on the reading half of the server socket. The client Stdout process can then exit. Both client processes have now exited. The sequence is: client Stdin sees EOF -> shutdown(), client Stdin exits -> server sees EOF -> close() -> client Stdout sees EOF -> client Stdout exits WARNING: There are some things that can prevent EOF from being seen by a client. If the server forks after an accept(), both processes (parent and child) have a copy of the open file descriptor. The child process will be the one reading/writing it; so, the parent *must* close its duplicate copy of the descriptor. If the parent keeps an open copy of the file descriptor, then even if the child closes its copy of the descriptor the remote client won't see an EOF because the parent still has the descriptor open. Make sure the parent closes its duplicate copy of the descriptor.