TcpListener.BeginAcceptSocket メソッド
アセンブリ: System (system.dll 内)

Public Function BeginAcceptSocket ( _ callback As AsyncCallback, _ state As Object _ ) As IAsyncResult
Dim instance As TcpListener Dim callback As AsyncCallback Dim state As Object Dim returnValue As IAsyncResult returnValue = instance.BeginAcceptSocket(callback, state)
戻り値
Socket の非同期の作成を参照する IAsyncResult。


非同期の BeginAcceptSocket 操作は、EndAcceptSocket メソッドを呼び出して終了させる必要があります。通常、このメソッドは callback デリゲートによって呼び出されます。
このメソッドは、操作が完了するまでブロックしません。操作が完了するまでブロックするには、AcceptSocket メソッドを使用します。
非同期プログラミング モデルの使用法の詳細については、「同期メソッドの非同期呼び出し」を参照してください。
![]() |
---|
返された Socket の RemoteEndPoint プロパティを呼び出すと、リモート ホストのネットワーク アドレスとポート番号を確認できます。 |

BeginAcceptSocket メソッドを使用して、ソケットの作成と接続を行うコード例を次に示します。コールバック デリゲートは EndAcceptSocket メソッドを呼び出して非同期要求を終了します。
' Thread signal. Public Shared clientConnected As New ManualResetEvent(False) ' Accept one client connection asynchronously. Public Shared Sub DoBeginAcceptSocket(listener As TcpListener) ' Set the event to nonsignaled state. clientConnected.Reset() ' Start to listen for connections from a client. Console.WriteLine("Waiting for a connection...") ' Accept the connection. ' BeginAcceptSocket() creates the accepted socket. listener.BeginAcceptSocket(New AsyncCallback(AddressOf DoAcceptSocketCallback), listener) ' Wait until a connection is made and processed before ' continuing. clientConnected.WaitOne() End Sub 'DoBeginAcceptSocket ' Process the client connection. Public Shared Sub DoAcceptSocketCallback(ar As IAsyncResult) ' Get the listener that handles the client request. Dim listener As TcpListener = CType(ar.AsyncState, TcpListener) ' End the operation and display the received data on the 'console. Dim clientSocket As Socket = listener.EndAcceptSocket(ar) ' Process the connection here. (Add the client to a ' server table, read data, etc.) Console.WriteLine("Client connected completed") ' Signal the calling thread to continue. clientConnected.Set() End Sub 'DoAcceptSocketCallback
// Thread signal. public static ManualResetEvent clientConnected = new ManualResetEvent(false); // Accept one client connection asynchronously. public static void DoBeginAcceptSocket(TcpListener listener) { // Set the event to nonsignaled state. clientConnected.Reset(); // Start to listen for connections from a client. Console.WriteLine("Waiting for a connection..."); // Accept the connection. // BeginAcceptSocket() creates the accepted socket. listener.BeginAcceptSocket( new AsyncCallback(DoAcceptSocketCallback), listener); // Wait until a connection is made and processed before // continuing. clientConnected.WaitOne(); } // Process the client connection. public static void DoAcceptSocketCallback(IAsyncResult ar) { // Get the listener that handles the client request. TcpListener listener = (TcpListener) ar.AsyncState; // End the operation and display the received data on the //console. Socket clientSocket = listener.EndAcceptSocket(ar); // Process the connection here. (Add the client to a // server table, read data, etc.) Console.WriteLine("Client connected completed"); // Signal the calling thread to continue. clientConnected.Set(); }
// Thread signal. public: static ManualResetEvent^ ClientConnected; // Accept one client connection asynchronously. public: static void DoBeginAcceptSocket(TcpListener^ listener) { // Set the event to nonsignaled state. ClientConnected->Reset(); // Start to listen for connections from a client. Console::WriteLine("Waiting for a connection..."); // Accept the connection. // BeginAcceptSocket() creates the accepted socket. listener->BeginAcceptSocket( gcnew AsyncCallback(DoAcceptSocketCallback), listener); // Wait until a connection is made and processed before // continuing. ClientConnected->WaitOne(); } // Process the client connection. public: static void DoAcceptSocketCallback(IAsyncResult^ result) { // Get the listener that handles the client request. TcpListener^ listener = (TcpListener^) result->AsyncState; // End the operation and display the received data on the //console. Socket^ clientSocket = listener->EndAcceptSocket(result); // Process the connection here. (Add the client to a // server table, read data, etc.) Console::WriteLine("Client connected completed"); // Signal the calling thread to continue. ClientConnected->Set(); }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- TcpListener.BeginAcceptSocket メソッドのページへのリンク