TcpListener.AcceptSocket Método

Definición

Acepta una solicitud de conexión pendiente.Accepts a pending connection request.

public:
 System::Net::Sockets::Socket ^ AcceptSocket();
public System.Net.Sockets.Socket AcceptSocket ();
member this.AcceptSocket : unit -> System.Net.Sockets.Socket
Public Function AcceptSocket () As Socket

Devoluciones

Socket

Socket que se utiliza para enviar y recibir datos.A Socket used to send and receive data.

Excepciones

El agente de escucha no se ha iniciado con una llamada a Start().The listener has not been started with a call to Start().

Ejemplos

En el ejemplo de código siguiente, el AcceptSocket método se utiliza para devolver un valor Socket .In the following code example, the AcceptSocket method is used to return a Socket. SocketSe utiliza para comunicarse con el cliente recién conectado.This Socket is used to communicate with the newly connected client.

// Accepts the pending client connection and returns a socket for communciation.
Socket^ socket = tcpListener->AcceptSocket();
Console::WriteLine( "Connection accepted." );

String^ responseString = "You have successfully connected to me";

//Forms and sends a response string to the connected client.
array<Byte>^sendBytes = Encoding::ASCII->GetBytes( responseString );
int i = socket->Send( sendBytes );
Console::WriteLine( "Message Sent /> : {0}", responseString );

          // Accepts the pending client connection and returns a socket for communciation.
           Socket socket = tcpListener.AcceptSocket();
            Console.WriteLine("Connection accepted.");

           string responseString = "You have successfully connected to me";

           //Forms and sends a response string to the connected client.
           Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString);
           int i = socket.Send(sendBytes);
           Console.WriteLine("Message Sent /> : " + responseString);
' Accepts the pending client connection and returns a socket for communciation.
Dim socket As Socket = tcpListener.AcceptSocket()
Console.WriteLine("Connection accepted.")

Dim responseString As String = "You have successfully connected to me"

'Forms and sends a response string to the connected client.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
Dim i As Integer = socket.Send(sendBytes)
Console.WriteLine(("Message Sent /> : " + responseString))

Comentarios

AcceptSocket es un método de bloqueo que devuelve un Socket que se puede usar para enviar y recibir datos.AcceptSocket is a blocking method that returns a Socket that you can use to send and receive data. Si desea evitar el bloqueo, use el Pending método para determinar si las solicitudes de conexión están disponibles en la cola de conexiones entrantes.If you want to avoid blocking, use the Pending method to determine if connection requests are available in the incoming connection queue.

El Socket devuelto se inicializa con la dirección IP y el número de puerto del host remoto.The Socket returned is initialized with the IP address and port number of the remote host. Puede usar cualquiera de los Send métodos y Receive disponibles en la Socket clase para comunicarse con el host remoto.You can use any of the Send and Receive methods available in the Socket class to communicate with the remote host. Cuando haya terminado de usar Socket , asegúrese de llamar a su Close método.When you are finished using the Socket, be sure to call its Close method. Si la aplicación es relativamente sencilla, considere la posibilidad de usar el AcceptTcpClient método en lugar del AcceptSocket método.If your application is relatively simple, consider using the AcceptTcpClient method rather than the AcceptSocket method. TcpClient proporciona métodos sencillos para enviar y recibir datos a través de una red en modo de bloqueo sincrónico.TcpClient provides you with simple methods for sending and receiving data over a network in blocking synchronous mode.

Nota

Este miembro genera información de seguimiento cuando se habilita el seguimiento de red en la aplicación.This member outputs trace information when you enable network tracing in your application. Para obtener más información, consulte seguimiento de red en el .NET Framework.For more information, see Network Tracing in the .NET Framework.

Se aplica a

Consulte también