TcpListener.AcceptSocket メソッド

定義

保留中の接続要求を受け入れます。

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

戻り値

Socket

データの送受信に使用する Socket

例外

リスナーが、Start() への呼び出しで開始されていません。

次のコード例では、メソッドを AcceptSocket 使用して Socket. これは Socket 、新しく接続されたクライアントと通信するために使用されます。

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

注釈

AcceptSocket は、データの送受信に使用できるメソッドを Socket 返すブロック メソッドです。 ブロックを回避する場合は、このメソッドを Pending 使用して、受信接続キューで接続要求を使用できるかどうかを判断します。

Socket返された値は、リモート ホストの IP アドレスとポート番号で初期化されます。 クラスで使用できる任意のSendReceiveメソッドを使用して、Socketリモート ホストと通信できます。 使用が Socket完了したら、必ずその Close メソッドを呼び出してください。 アプリケーションが比較的単純な場合は、メソッドではなくメソッドをAcceptTcpClientAcceptSocket使用することを検討してください。 TcpClient では、ブロック同期モードでネットワーク経由でデータを送受信するための簡単な方法が提供されます。

注意

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください。

適用対象

こちらもご覧ください