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

Public Function BeginAcceptTcpClient ( _ 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.BeginAcceptTcpClient(callback, state)
戻り値
TcpClient の非同期の作成を参照する IAsyncResult。


非同期の BeginAcceptTcpClient 操作は、EndAcceptTcpClient メソッドを呼び出して終了させる必要があります。通常、このメソッドは callback デリゲートによって呼び出されます。
このメソッドは、操作が完了するまでブロックしません。操作が完了するまでブロックするには、AcceptTcpClient メソッドを使用します。
非同期プログラミング モデルの使用法の詳細については、「同期メソッドの非同期呼び出し」を参照してください。
![]() |
---|
SocketException が発生した場合は、SocketException.ErrorCode プロパティを使用して特定のエラー コードを取得してください。エラーの詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Socket Version 2 API のエラー コードのドキュメントを参照してください。 |

BeginAcceptTcpClient メソッドを使用して、ソケットの作成と接続を行うコード例を次に示します。コールバック デリゲートは EndAcceptTcpClient メソッドを呼び出して非同期要求を終了します。
' Thread signal. Public Shared tcpClientConnected As New ManualResetEvent(False) ' Accept one client connection asynchronously. Public Shared Sub DoBeginAcceptTcpClient(listener As TcpListener) ' Set the event to nonsignaled state. tcpClientConnected.Reset() ' Start to listen for connections from a client. Console.WriteLine("Waiting for a connection...") ' Accept the connection. ' BeginAcceptSocket() creates the accepted socket. listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptTcpClientCallback), listener) ' Wait until a connection is made and processed before ' continuing. tcpClientConnected.WaitOne() End Sub 'DoBeginAcceptTcpClient ' Process the client connection. Public Shared Sub DoAcceptTcpClientCallback(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 client As TcpClient = listener.EndAcceptTcpClient(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. tcpClientConnected.Set() End Sub 'DoAcceptTcpClientCallback
// Thread signal. public static ManualResetEvent tcpClientConnected = new ManualResetEvent(false); // Accept one client connection asynchronously. public static void DoBeginAcceptTcpClient(TcpListener listener) { // Set the event to nonsignaled state. tcpClientConnected.Reset(); // Start to listen for connections from a client. Console.WriteLine("Waiting for a connection..."); // Accept the connection. // BeginAcceptSocket() creates the accepted socket. listener.BeginAcceptTcpClient( new AsyncCallback(DoAcceptTcpClientCallback), listener); // Wait until a connection is made and processed before // continuing. tcpClientConnected.WaitOne(); } // Process the client connection. public static void DoAcceptTcpClientCallback(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. TcpClient client = listener.EndAcceptTcpClient(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. tcpClientConnected.Set(); }
// Thread signal. public: static ManualResetEvent^ TcpClientConnected; // Accept one client connection asynchronously. public: static void DoBeginAcceptTcpClient(TcpListener^ listener) { // Set the event to nonsignaled state. TcpClientConnected->Reset(); // Start to listen for connections from a client. Console::WriteLine("Waiting for a connection..."); // Accept the connection. // BeginAcceptSocket() creates the accepted socket. listener->BeginAcceptTcpClient( gcnew AsyncCallback(DoAcceptTcpClientCallback), listener); // Wait until a connection is made and processed before // continuing. TcpClientConnected->WaitOne(); } // Process the client connection. public: static void DoAcceptTcpClientCallback(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. TcpClient^ client = listener->EndAcceptTcpClient(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. TcpClientConnected->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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTcpListener.BeginAcceptTcpClient メソッドを検索する場合は、下記のリンクをクリックしてください。

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