If you want to close the stream in one direction only, use the shutdown socket call instead of the close() call. On the shutdown() call, you can specify the direction in which the stream is to be closed.
When a shutdown() call is issued for receive and there is unread data queued to the socket, the connection is aborted. If data arrives inbound on a connection that has been shut down for receive, the connection is aborted. When the connection is aborted, all outstanding socket calls on the socket will be posted with an ECONNABORTED error. The abort discards all unsent and unreceived data on the local and remote end of the connection, and the connection is destroyed. The application should issue a close() on the socket.
See Table 1 for a list of the effect on read and write calls when the stream is shut down in one or both directions.
Socket calls in local program | Local program | Remote program | ||
---|---|---|---|---|
Shutdown SEND | Shutdown RECEIVE | Shutdown RECEIVE | Shutdown SEND | |
Write calls | Error number EPIPE on first call | Error number EPIPE on second call* | ||
Read calls | Zero length return code | Zero length return code | ||
* If you issue two write calls immediately, both might be successful, and an EPIPE error number might not be returned until a third write call is issued. |