Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Método Socket.BeginDisconnect (Boolean, AsyncCallback, Object)

 

Inicia uma solicitação assíncrona para se desconectar de um ponto de extremidade remoto.

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

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

Parâmetros

reuseSocket
Type: System.Boolean

true se o soquete puder ser reutilizado após a conexão ser fechada; caso contrário, false.

callback
Type: System.AsyncCallback

O delegado AsyncCallback.

state
Type: System.Object

Um objeto que contém informações de estado para essa solicitação.

Valor Retornado

Type: System.IAsyncResult

Um objeto IAsyncResult que faz referência à operação assíncrona.

Exception Condition
NotSupportedException

O sistema operacional é o Windows 2000 ou anterior e esse método requer o Windows XP.

ObjectDisposedException

O objeto Socket foi fechado.

SocketException

Ocorreu um erro ao tentar acessar o soquete. Consulte a seção Comentários para obter mais informações.

If you are using a connection-oriented protocol, you can call the M:System.Net.Sockets.Socket.BeginDisconnect(System.Boolean,System.AsyncCallback,System.Object) method to request a disconnect from a remote endpoint. If reuseSocket is true, you can reuse the socket.

The M:System.Net.Sockets.Socket.BeginDisconnect(System.Boolean,System.AsyncCallback,System.Object) method uses a separate thread to invoke the specified callback method. The M:System.Net.Sockets.Socket.EndDisconnect(System.IAsyncResult) method blocks until the pending disconnect is complete. For additional information on writing callback methods, see [<topic://cpconcallbacksample>].

System_CAPS_noteObservação

If you receive a T:System.Net.Sockets.SocketException exception, use the P:System.Net.Sockets.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.

System_CAPS_noteObservação

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

The following code example creates a socket for asynchronous communication and sends some data to a remote host. When the data has been sent, M:System.Net.Sockets.Socket.Shutdown(System.Net.Sockets.SocketShutdown) is called to stop the send and receive activity. Then M:System.Net.Sockets.Socket.BeginDisconnect(System.Boolean,System.AsyncCallback,System.Object) is called to begin a disconnect request. When the request completes, the P:System.Net.Sockets.Socket.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();
}

.NET Framework
Disponível desde 2.0
Retornar ao início
Mostrar: