close vs shutdown socket?

user188276 picture user188276 · Nov 12, 2010 · Viewed 250.1k times · Source

In C, I understood that if we close a socket, it means the socket will be destroyed and can be re-used later.

How about shutdown? The description said it closes half of a duplex connection to that socket. But will that socket be destroyed like close system call?

Answer

Matthew Flaschen picture Matthew Flaschen · Nov 12, 2010

This is explained in Beej's networking guide. shutdown is a flexible way to block communication in one or both directions. When the second parameter is SHUT_RDWR, it will block both sending and receiving (like close). However, close is the way to actually destroy a socket.

With shutdown, you will still be able to receive pending data the peer already sent (thanks to Joey Adams for noting this).