What is the difference and which rules I must follow when working with socks? I'm writing simple daemon, which must listen port and do some actions.
Asked
Active
Viewed 8,434 times
3
-
3Did you check the MSDN pages for both methods? – CodeCaster Jun 24 '15 at 16:22
-
3Pro answers is allways better. Maybe I've missed something? – Arman Hayots Jun 24 '15 at 16:24
-
See also regarding the proper way to clean up a Socket: https://stackoverflow.com/a/62204814/222054 – Kelly Elton Jun 04 '20 at 21:57
1 Answers
9
Socket.Close
calls Dispose
(but it's undocumented).
When using a connection-oriented Socket, always call the Shutdown method before closing the Socket. This ensures that all data is sent and received on the connected socket before it is closed. (msdn)
Your code should looks like this (at least I'd do it like this):
using (var socket = new Socket())
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
The Disconnect
method takes a single parameter bool reuseSocket
, according to msdn:
reuseSocket Type: System.Boolean true if this socket can be reused after the current connection is closed; otherwise, false.
which basically means, when you set reuseSocket
to false
it will be disposed after you close it.
The Shutdown
method will not disconnect your socket, it will just disable sending/receiving data.

Jeff LaFay
- 12,882
- 13
- 71
- 101

voytek
- 2,202
- 3
- 28
- 44