Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Socket.Disconnect Method

Closes the socket connection and allows reuse of the socket.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public void Disconnect (
	bool reuseSocket
)
public void Disconnect (
	boolean reuseSocket
)
public function Disconnect (
	reuseSocket : boolean
)
Not applicable.

Parameters

reuseSocket

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

Exception typeCondition

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 DontLingerSocket 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.

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.

IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress  ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

Socket client = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);

// Connect the socket to the remote end point.
client.Connect(ipEndPoint);

// Send some data to the remote device.
string data = "This is a string of data <EOF>";
byte[] buffer = Encoding.ASCII.GetBytes(data);

int bytesTransferred =  client.Send(buffer);

// Write to the console the number of bytes transferred.
Console.WriteLine("{0} bytes were sent.\n", bytesTransferred);

// Release the socket.
client.Shutdown(SocketShutdown.Both);
          
client.Disconnect(true);
if (client.Connected) 
    Console.WriteLine("We're still connnected");
else 
    Console.WriteLine("We're disconnected");

IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.get_AddressList()[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

Socket client = new Socket(AddressFamily.InterNetwork, 
    SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote end point.
client.Connect(ipEndPoint);
// Send some data to the remote device.
String data = "This is a string of data <EOF>";
ubyte buffer[] = Encoding.get_ASCII().GetBytes(data);

int bytesTransferred = client.Send(buffer);
// Write to the console the number of bytes transferred.
Console.WriteLine("{0} bytes were sent.\n", 
    (System.Int32)bytesTransferred);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Disconnect(true);
if (client.get_Connected()) {
    Console.WriteLine("We're still connnected");
}
else {
    Console.WriteLine("We're disconnected");
} 

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft