When using Poco's StreamSocket
class and you want to close the socket connection, it is unclear from both the Poco documentation and the function implementations whether Socket::close()
or StreamSocket::shutdown()
would be the most appropriate function to call. On platforms where these appear to forward through to POSIX functions, the man page for shutdown()
states that sending/receiving will be stopped, but says nothing about the connection itself. Would I be safe in assuming that close()
is essentially going to be a superset of what shutdown()
does and hence close()
is the more appropriate one to call? If so, is there an example scenario where shutdown()
would be the more appropriate call?

- 9,238
- 5
- 56
- 85
2 Answers
The shutdown
function shuts down either the sending side of the connection, the receiving side of the connection, or both. The close
function just closes the socket and doesn't necessarily have any effect on the connection. However, if the connection has no references (because you close
d the last one), then the operating system will shut down the connection.

- 179,497
- 17
- 214
- 278
-
I've accepted this as the answer since it is the most technically correct. The answer by @865719 contains links which provide considerably more detail and useful background which complements David's answer. – Craig Scott Oct 13 '15 at 22:22
According to POCO's introductory slides on Network Programming:
POCO sockets are a very thin layer on top of BSD sockets
-- Slide #7
So POCO sockets adopt BSD socket's semantics for close()
and shutdown()
.
As mentioned in this SO answer (which refers to Beej's guide to network programming):
whereas close()
will prevent reads/writes on the remote socketshutdown()
gives you more control on which end is to be cut off. (which explains the presence of StreamSocket::shutdownSend()
and StreamSocket::shutdownReceive()
alongside StreamSocket::shutdown()
)
EDIT: make sure to check out David Schwartz's comment about the accuracy of Beej's guide concerning the actual effects of close()
.

- 3,737
- 5
- 29
- 51
-
The `close` function doesn't necessarily prevent reads or writes like `shutdown` does. – David Schwartz Oct 12 '15 at 08:28
-
I was quoting [Beej's guide](http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#closedown): "`close(sockfd);`: This will prevent any more reads and writes to the socket. Anyone attempting to read or write the socket on the remote end will receive an error." – maddouri Oct 12 '15 at 08:30
-
2Yes, but you said "on both ends of the socket", implying that it has an effect on the connection. Now you say the "remote socket", which it also doesn't necessarily have any effect on. – David Schwartz Oct 12 '15 at 08:31
-
-
Beej is wrong. You can see that he's wrong by `dup`ing the socket, then `close`ing it, then trying to `write` on the remote end. It will work just fine. – David Schwartz Oct 12 '15 at 08:34
-
Interesting, I'll make sure to check it out later. Thanks for the comment. – maddouri Oct 12 '15 at 08:37