TcpListener.AcceptSocket Metodo

Definizione

Accetta una richiesta di connessione in sospeso.

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

Restituisce

Oggetto Socket utilizzato per inviare e ricevere dati.

Eccezioni

Il listener non è stato avviato con una chiamata a Start().

Esempio

Nell'esempio di codice seguente viene usato il AcceptSocket metodo per restituire un oggetto Socket. Viene Socket usato per comunicare con il client appena connesso.

// 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 communication.
           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))

Commenti

AcceptSocket è un metodo di blocco che restituisce un Socket oggetto che è possibile usare per inviare e ricevere dati. Se si vuole evitare il blocco, usare il Pending metodo per determinare se le richieste di connessione sono disponibili nella coda di connessione in ingresso.

L'oggetto Socket restituito viene inizializzato con l'indirizzo IP e il numero di porta dell'host remoto. È possibile usare uno dei Send metodi e Receive disponibili nella Socket classe per comunicare con l'host remoto. Al termine dell'uso di Socket, assicurarsi di chiamare il relativo Close metodo. Se l'applicazione è relativamente semplice, è consigliabile usare il AcceptTcpClient metodo anziché il AcceptSocket metodo . TcpClient fornisce metodi semplici per l'invio e la ricezione di dati in una rete in modalità sincrona.

Nota

Questo membro genera informazioni di traccia quando viene abilitata la funzionalità di traccia di rete nell'applicazione in uso. Per altre informazioni, vedere Traccia di rete in .NET Framework.

Si applica a

Vedi anche