Socket.EndAccept メソッド (Byte[], Int32, IAsyncResult)
アセンブリ: System (system.dll 内)

Public Function EndAccept ( _ <OutAttribute> ByRef buffer As Byte(), _ <OutAttribute> ByRef bytesTransferred As Integer, _ asyncResult As IAsyncResult _ ) As Socket
Dim instance As Socket Dim buffer As Byte() Dim bytesTransferred As Integer Dim asyncResult As IAsyncResult Dim returnValue As Socket returnValue = instance.EndAccept(buffer, bytesTransferred, asyncResult)
public: Socket^ EndAccept ( [OutAttribute] array<unsigned char>^% buffer, [OutAttribute] int% bytesTransferred, IAsyncResult^ asyncResult )
public Socket EndAccept ( /** @attribute OutAttribute() */ /** @ref */ byte[] buffer, /** @attribute OutAttribute() */ /** @ref */ int bytesTransferred, IAsyncResult asyncResult )
戻り値
リモート ホストとの通信を処理する Socket オブジェクト。

例外の種類 | 条件 |
---|---|
NotSupportedException | このメソッドには Windows NT が必要です。 |
ObjectDisposedException | |
ArgumentNullException | asyncResult が空です。 |
ArgumentException | |
InvalidOperationException | |
SocketException |

EndAccept は BeginAccept の呼び出しを完了します。BeginAccept を呼び出す前に、AsyncCallback デリゲートによって呼び出されるコールバック メソッドを作成する必要があります。このコールバック メソッドは個別のスレッドで実行され、BeginAccept メソッドが返された後で呼び出されます。このコールバック メソッドは、BeginAccept メソッドから返された asyncResult パラメータを受け取る必要があります。
コールバック メソッド内では、asyncResult パラメータの AsyncState メソッドを呼び出して、接続が試行された Socket を取得します。Socket を取得したら、EndAccept メソッドを呼び出して接続の試行を正常に完了できます。このオーバーロードの buffer パラメータには、BeginAccept の呼び出しで受信されたデータが格納されます。bytesTransferred パラメータには、その呼び出しで転送されたバイト数が格納されます。
EndAccept メソッドは、受信接続キューで接続が保留中の間はブロックします。EndAccept メソッドは受信接続を受け入れ、リモート ホストとのデータの送受信で使用できる新しい Socket を返します。
![]() |
---|
SocketException が発生した場合は、SocketException.ErrorCode プロパティを使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |

BeginAccept を使用してソケットの作成と接続を行い、データの最初の 10 バイトを受け取るコード例を次に示します。コールバック デリゲートは EndAccept を呼び出し、非同期要求を終了します。転送されたバイト数とデータは、このメソッドの buffer パラメータと bytesTransferred パラメータに返され、コンソールにも表示されます。
// This server waits for a connection and then uses asynchronous operations to // accept the connection with initial data sent from the client. // Establish the local endpoint for the socket. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Bind the socket to the local endpoint, and listen for incoming connections. listener.Bind(localEndPoint); listener.Listen(100); while (true) { // Set the event to nonsignaled state. allDone.Reset(); // Start an asynchronous socket to listen for connections and receive data from the client. Console.WriteLine("Waiting for a connection..."); // Accept the connection and receive the first 10 bytes of data. int receivedDataSize = 10; listener.BeginAccept(receivedDataSize, new AsyncCallback(AcceptReceiveCallback), listener); // Wait until a connection is made and processed before continuing. allDone.WaitOne(); } } public static void AcceptReceiveCallback(IAsyncResult ar) { // Get the socket that handles the client request. Socket listener = (Socket) ar.AsyncState; // End the operation and display the received data on the console. byte[] Buffer; int bytesTransferred; Socket handler = listener.EndAccept(out Buffer, out bytesTransferred, ar); string stringTransferred = Encoding.ASCII.GetString(Buffer, 0, bytesTransferred); Console.WriteLine(stringTransferred); Console.WriteLine("Size of data transferred is {0}", bytesTransferred); // Create the state object for the asynchronous receive. StateObject state = new StateObject(); state.workSocket = handler; handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Socket.EndAccept メソッド (IAsyncResult)
アセンブリ: System (system.dll 内)

Dim instance As Socket Dim asyncResult As IAsyncResult Dim returnValue As Socket returnValue = instance.EndAccept(asyncResult)
戻り値
リモート ホストとの通信を処理する Socket。

例外の種類 | 条件 |
---|---|
ArgumentNullException | asyncResult が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | |
SocketException | |
ObjectDisposedException | |
InvalidOperationException | |
NotSupportedException | このメソッドには Windows NT が必要です。 |

EndAccept は BeginAccept の呼び出しを完了します。BeginAccept を呼び出す前に、AsyncCallback デリゲートを実装するコールバック メソッドを作成する必要があります。このコールバック メソッドは個別のスレッドで実行され、BeginAccept メソッドが返された後で呼び出されます。このコールバック メソッドは、BeginAccept メソッドから返された asyncResult パラメータを受け取る必要があります。
コールバック メソッド内では、asyncResult パラメータの AsyncState メソッドを呼び出して、接続が試行された Socket を取得します。Socket を取得したら、EndAccept メソッドを呼び出して接続の試行を正常に完了できます。
EndAccept メソッドは、受信接続キューで接続が保留中の間はブロックします。EndAccept メソッドは受信接続を受け入れ、リモート ホストとのデータの送受信で使用できる新しい Socket を返します。
![]() |
---|
SocketException が発生した場合は、SocketException.ErrorCode プロパティを使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |

非同期要求を終了し、受信接続要求を受け入れる新しい Socket を作成するコード例を次に示します。ソケットを使用した非同期通信を示すコード例の全体については、「ソケットのコード例」を参照してください。
Public Shared Sub Listen_Callback(ar As IAsyncResult) allDone.Set() Dim s As Socket = CType(ar.AsyncState, Socket) Dim s2 As Socket = s.EndAccept(ar) Dim so2 As New StateObject() so2.workSocket = s2 s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE, 0, New AsyncCallback(AddressOf Async_Send_Receive.Read_Callback), so2) End Sub 'Listen_Callback
public static void Listen_Callback(IAsyncResult ar){ allDone.Set(); Socket s = (Socket) ar.AsyncState; Socket s2 = s.EndAccept(ar); StateObject so2 = new StateObject(); so2.workSocket = s2; s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0, new AsyncCallback(Async_Send_Receive.Read_Callback), so2); }
static void Listen_Callback( IAsyncResult^ ar ) { allDone->Set(); Socket^ s = safe_cast<Socket^>(ar->AsyncState); Socket^ s2 = s->EndAccept( ar ); StateObject^ so2 = gcnew StateObject; so2->workSocket = s2; s2->BeginReceive( so2->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None , gcnew AsyncCallback( &Async_Send_Receive::Read_Callback ), so2 ); }

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


Socket.EndAccept メソッド

名前 | 説明 |
---|---|
Socket.EndAccept (IAsyncResult) | 受信接続の試行を非同期的に受け入れ、新しい Socket を作成してリモート ホスト通信を処理します。 .NET Compact Framework によってサポートされています。 |
Socket.EndAccept (Byte[], IAsyncResult) | 受信接続の試行を非同期的に受け入れ、新しい Socket オブジェクトを作成してリモート ホスト通信を処理します。このメソッドは、転送される初期データを格納するバッファを返します。 |
Socket.EndAccept (Byte[], Int32, IAsyncResult) | 受信接続の試行を非同期的に受け入れ、新しい Socket オブジェクトを作成してリモート ホスト通信を処理します。このメソッドは、初期データと、転送されるバイト数を格納するバッファを返します。 |

Socket.EndAccept メソッド (Byte[], IAsyncResult)
アセンブリ: System (system.dll 内)

Public Function EndAccept ( _ <OutAttribute> ByRef buffer As Byte(), _ asyncResult As IAsyncResult _ ) As Socket
Dim instance As Socket Dim buffer As Byte() Dim asyncResult As IAsyncResult Dim returnValue As Socket returnValue = instance.EndAccept(buffer, asyncResult)
public: Socket^ EndAccept ( [OutAttribute] array<unsigned char>^% buffer, IAsyncResult^ asyncResult )
public Socket EndAccept ( /** @attribute OutAttribute() */ /** @ref */ byte[] buffer, IAsyncResult asyncResult )
戻り値
リモート ホストとの通信を処理する Socket オブジェクト。

例外の種類 | 条件 |
---|---|
NotSupportedException | このメソッドには Windows NT が必要です。 |
ObjectDisposedException | |
ArgumentNullException | asyncResult が空です。 |
ArgumentException | |
InvalidOperationException | |
SocketException |

EndAccept は BeginAccept の呼び出しを完了します。BeginAccept を呼び出す前に、AsyncCallback デリゲートによって呼び出されるコールバック メソッドを作成する必要があります。このコールバック メソッドは個別のスレッドで実行され、BeginAccept メソッドが返された後で呼び出されます。
コールバック メソッド内では、asyncResult パラメータの AsyncState メソッドを呼び出して、接続が試行された Socket を取得します。Socket を取得したら、EndAccept メソッドを呼び出して接続の試行を正常に完了できます。このオーバーロードの buffer パラメータには、BeginAccept の呼び出しで受信されたデータが格納されます。bytesTransferred パラメータには、その呼び出しで転送されたバイト数が格納されます。
EndAccept メソッドは、受信接続キューで接続が保留中の間はブロックします。EndAccept メソッドは受信接続を受け入れ、リモート ホストとのデータの送受信で使用できる新しい Socket を返します。
![]() |
---|
SocketException が発生した場合は、SocketException.ErrorCode プロパティを使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |

BeginAccept を使用してソケットの作成と接続を行い、データの最初の 10 バイトを受け取るコード例を次に示します。コールバック デリゲートは EndAccept を呼び出し、非同期要求を終了します。転送されたバイト数とデータは、このメソッドの buffer パラメータと bytesTransferred パラメータに返され、コンソールにも表示されます。
// This server waits for a connection and then uses asynchronous operations to // accept the connection with initial data sent from the client. // Establish the local endpoint for the socket. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Bind the socket to the local endpoint, and listen for incoming connections. listener.Bind(localEndPoint); listener.Listen(100); while (true) { // Set the event to nonsignaled state. allDone.Reset(); // Start an asynchronous socket to listen for connections and receive data from the client. Console.WriteLine("Waiting for a connection..."); // Accept the connection and receive the first 10 bytes of data. int receivedDataSize = 10; listener.BeginAccept(receivedDataSize, new AsyncCallback(AcceptReceiveCallback), listener); // Wait until a connection is made and processed before continuing. allDone.WaitOne(); } } public static void AcceptReceiveCallback(IAsyncResult ar) { // Get the socket that handles the client request. Socket listener = (Socket) ar.AsyncState; // End the operation and display the received data on the console. byte[] Buffer; int bytesTransferred; Socket handler = listener.EndAccept(out Buffer, out bytesTransferred, ar); string stringTransferred = Encoding.ASCII.GetString(Buffer, 0, bytesTransferred); Console.WriteLine(stringTransferred); Console.WriteLine("Size of data transferred is {0}", bytesTransferred); // Create the state object for the asynchronous receive. StateObject state = new StateObject(); state.workSocket = handler; handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Socket.EndAcceptのページへのリンク