Export (0) Print
Expand All

Socket.BeginDisconnect Method

Begins an asynchronous request to disconnect from a remote endpoint.

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

[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public IAsyncResult BeginDisconnect(
	bool reuseSocket,
	AsyncCallback callback,
	Object state
)

Parameters

reuseSocket
Type: System.Boolean

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

callback
Type: System.AsyncCallback

The AsyncCallback delegate.

state
Type: System.Object

An object that contains state information for this request.

Return Value

Type: System.IAsyncResult
An IAsyncResult object that references the asynchronous operation.

ExceptionCondition
NotSupportedException

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

ObjectDisposedException

The Socket object has been closed.

SocketException

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

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

If you are using a connection-oriented protocol, you can call the BeginDisconnect method to request a disconnect from a remote endpoint. If reuseSocket is true, you can reuse the socket.

The BeginDisconnect method uses a separate thread to invoke the specified callback method. The EndDisconnect method blocks until the pending disconnect is complete. For additional information on writing callback methods, see Callback Sample.

NoteNote:

If you receive a SocketException exception, 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. 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::Resolve(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(0, ConnectCallback), client);

      // Wait for connect.
      connectDone->WaitOne();

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

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

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


private:
   static void DisconnectCallback(IAsyncResult* ar)
   { 
      // Complete the disconnect request.
      Socket* client = dynamic_cast<Socket*> (ar->AsyncState);
      client->EndDisconnect(ar);

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft