System.Net.Sockets.Socket.Disconnect Method

Closes the socket connection and allows reuse of the socket.

Syntax

public void Disconnect (bool reuseSocket)

Parameters

reuseSocket
true if this socket can be reused after the current connection is closed; otherwise, false.

Remarks

If you are using a connection-oriented protocol, you can use this method to close the socket. This method ends the connection and sets the Socket.Connected property to false. However, if reuseSocket is true, you can reuse the socket.

To ensure that all data is sent and received before the socket is closed, you should call Socket.Shutdown(SocketShutdown) before calling the Socket.Disconnect(bool) method.

If you need to call Socket.Disconnect(bool) without first calling Socket.Shutdown(SocketShutdown), you can set the SocketOptionName.DontLinger System.Net.Sockets.Socket option to false and specify a nonzero time-out interval to ensure that data queued for outgoing transmission is sent. Socket.Disconnect(bool) then blocks until the 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.

Note:

If you receive a System.Net.Sockets.SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Note:

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

Requirements

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