Export (0) Print
Expand All

Socket.EndDisconnect Method

Ends a pending asynchronous disconnect request.

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

public void EndDisconnect (
	IAsyncResult asyncResult
)
public void EndDisconnect (
	IAsyncResult asyncResult
)
public function EndDisconnect (
	asyncResult : IAsyncResult
)
Not applicable.

Parameters

asyncResult

An IAsyncResult object that stores state information and any user-defined data for this asynchronous operation.

Exception typeCondition

NotSupportedException

The operating system is Windows 2000 or earlier, and this method requires Windows XP.

ObjectDisposedException

The Socket object has been closed.

ArgumentNullException

asyncResult is a null reference (Nothing in Visual Basic).

ArgumentException

asyncResult was not returned by a call to the BeginDisconnect method.

InvalidOperationException

EndDisconnect was previously called for the asynchronous connection.

SocketException

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

WebException

The disconnect request has timed out.

EndDisconnect completes a call to BeginDisconnect. The EndDisconnect method blocks until the disconnect completes. For information about asynchronous operations, see the Asynchronous Programming Overview topic in the MSDN library.

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 asynchronous communication and sends some data to a remote host. When the data has been sent, Shutdown is called to stop the send and receive activity. Then BeginDisconnect is called to begin a disconnect request. The callback delegate calls EndDisconnect to end the asynchronous request. When the request completes, the Connected property is queried to test whether the socket is disconnected.

    // Establish the remote endpoint for the socket.
    // For this example use local computer.
    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

    // Create a TCP/IP socket.
    Socket client = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);

    // Connect to the remote endpoint.
    client.BeginConnect(remoteEP, 
        new AsyncCallback(ConnectCallback), client);
        
    // Wait for connect.
    connectDone.WaitOne();

    // Send some data to the remote device.
    string data = "This is a string of data <EOF>";
    byte[] buffer = Encoding.ASCII.GetBytes(data);
    client.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(ClientSendCallback), client);
    // Wait for send done.
    sendDone.WaitOne();

    // Release the socket.
    client.Shutdown(SocketShutdown.Both);
    client.BeginDisconnect(true, new AsyncCallback(DisconnectCallback), client);

    // Wait for the disconnect to complete.
    disconnectDone.WaitOne();
    if (client.Connected)
        Console.WriteLine("We're still connected");
    else
        Console.WriteLine("We're disconnected");
}


private static void DisconnectCallback(IAsyncResult ar)
{ 
    // Complete the disconnect request.
    Socket client = (Socket) ar.AsyncState;
    client.EndDisconnect(ar);

    // Signal that the disconnect is complete.
    disconnectDone.Set();
}
     

    // Establish the remote endpoint for the socket.
    // For this example use local computer.
    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.get_AddressList()[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
    // Create a TCP/IP socket.
    Socket client = new Socket(AddressFamily.InterNetwork, 
        SocketType.Stream, ProtocolType.Tcp);
    // Connect to the remote endpoint.
    client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback),
        client);
    // Wait for connect.
    connectDone.WaitOne();
    // Send some data to the remote device.
    String data = "This is a string of data <EOF>";
    ubyte buffer[] = Encoding.get_ASCII().GetBytes(data);
    client.BeginSend(buffer, 0, buffer.get_Length(), (SocketFlags)0,
        new AsyncCallback(ClientSendCallback), client);
    // Wait for send done.
    sendDone.WaitOne();
    // Release the socket.
    client.Shutdown(SocketShutdown.Both);
    client.BeginDisconnect(true, new AsyncCallback(DisconnectCallback),
        client);
    // Wait for the disconnect to complete.
    disconnectDone.WaitOne();
    if (client.get_Connected()) {
        Console.WriteLine("We're still connected");
    }
    else {
        Console.WriteLine("We're disconnected");
    }
} //ClientDisconnect

private static void DisconnectCallback(IAsyncResult ar)
{
    // Complete the disconnect request.
    Socket client = (Socket)ar.get_AsyncState();
    client.EndDisconnect(ar);
    // Signal that the disconnect is complete.
    disconnectDone.Set();
} //DisconnectCallback

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

Community Additions

ADD
Show:
© 2014 Microsoft