Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Socket.Disconnect Method

Closes the socket connection and allows reuse of the socket.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)
'Declaration
Public Sub Disconnect ( _
	reuseSocket As Boolean _
)

Parameters

reuseSocket
Type: System.Boolean

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

ExceptionCondition
PlatformNotSupportedException

This method requires Windows 2000 or earlier, or the exception will be thrown.

ObjectDisposedException

The Socket object has been closed.

SocketException

An error occurred when attempting to access the socket. See the Remarks section for more information.

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 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 Shutdown before calling the Disconnect method.

If you need to call Disconnect without first calling Shutdown, you can set the DontLinger Socket option to false and specify a nonzero time-out interval to ensure that data queued for outgoing transmission is sent. Disconnect then blocks until the data is sent or until the specified time-out expires. If you set DontLinger to false and specify a zero time-out interval, Close releases the connection and automatically discards outgoing queued data.

NoteNote

If you receive a 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.

NoteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

The following code example creates a socket for synchronous communication and sends some data to a remote host. It then calls Shutdown, to stop the send and receive activity, and Disconnect, to close the socket connection.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.