System.Net.Sockets.Socket.Close Method

Closes the System.Net.Sockets.Socket connection and releases all associated resources.


public void Close ()


The Socket.Close method closes the remote host connection and releases all managed and unmanaged resources associated with the System.Net.Sockets.Socket. Upon closing, the Socket.Connected property is set to false.

For connection-oriented protocols, it is recommended that you call Socket.Shutdown(SocketShutdown) before calling the Socket.Close method. This ensures that all data is sent and received on the connected socket before it is closed.

If you need to call Socket.Close without first calling Socket.Shutdown(SocketShutdown), you can ensure that data queued for outgoing transmission will be sent by setting the SocketOptionName.DontLinger System.Net.Sockets.Socket option to false and specifying a non-zero time-out interval. Socket.Close will then block until this data is sent or until the specified time-out expires. If you set SocketOptionName.DontLinger to false and specify a zero time-out interval, Socket.Close releases the connection and automatically discards outgoing queued data.


To set the SocketOptionName.DontLinger socket option to false, create a System.Net.Sockets.LingerOption, set the enabled property to true, and set the LingerOption.LingerTime property to the desired time out period. Use this System.Net.Sockets.LingerOption along with the SocketOptionName.DontLinger socket option to call the Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) method.


This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].


Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0,,