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

Public Function EndReceiveFrom ( _ asyncResult As IAsyncResult, _ ByRef endPoint As EndPoint _ ) As Integer
Dim instance As Socket Dim asyncResult As IAsyncResult Dim endPoint As EndPoint Dim returnValue As Integer returnValue = instance.EndReceiveFrom(asyncResult, endPoint)
戻り値
正常に完了した場合は、受信したバイト数。失敗した場合は、0 を返します。


EndReceiveFrom メソッドは、BeginReceiveFrom メソッドで開始された非同期の読み取り操作を完了します。
BeginReceiveFrom を呼び出す前に、AsyncCallback デリゲートを実装するコールバック メソッドを作成する必要があります。このコールバック メソッドは個別のスレッドで実行され、BeginReceiveFrom の終了時に呼び出されます。コールバック メソッドは、BeginReceiveFrom メソッドからパラメータとして返された IAsyncResult を受け取る必要があります。
コールバック メソッド内では、IAsyncResult の AsyncState メソッドを呼び出して、BeginReceiveFrom に渡された状態オブジェクトを取得します。この状態オブジェクトから受信した Socket を抽出してください。Socket を取得したら、EndReceiveFrom メソッドを呼び出して読み取り操作を正常に完了し、読み取られたバイト数を返すことができます。
EndReceiveFrom メソッドは、データが使用可能になるまでブロックします。コネクションレスのプロトコルを使用している場合、EndReceiveFrom は、受信ネットワーク バッファ内で使用できる、最初にキューに格納されたデータグラムを読み取ります。コネクション指向のプロトコルを使用している場合、EndReceiveFrom メソッドは、BeginReceiveFrom メソッドの size パラメータで指定したバイト数までの、使用可能なデータをすべて読み取ります。リモート ホストが Shutdown メソッドで Socket 接続をシャットダウンし、使用できるデータがすべて受信されると、EndReceiveFrom メソッドはすぐに完了して、0 バイトを返します。受信したデータを取得するには、IAsyncResult オブジェクトの AsyncState メソッドを呼び出して、結果として得られる状態オブジェクト内のバッファを抽出します。送信元ホストを確認するには、EndPoint を抽出して IPEndPoint にキャストします。IPEndPoint.Address メソッドを使用すると IP アドレスを取得できます。IPEndPoint.Port メソッドはポート番号を取得するときに使用します。
![]() |
---|
SocketException が発生した場合は、SocketException.ErrorCode プロパティを使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |

特定の EndPoint からの保留中の非同期読み取りを終了するコード例を次に示します。
Dim so As StateObject = CType(ar.AsyncState, StateObject) Dim s As Socket = so.workSocket ' Creates a temporary EndPoint to pass to EndReceiveFrom. Dim sender As New IPEndPoint(IPAddress.Any, 0) Dim tempRemoteEP As EndPoint = CType(sender, EndPoint) Dim read As Integer = s.EndReceiveFrom(ar, tempRemoteEP) If read > 0 Then so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read)) s.BeginReceiveFrom(so.buffer, 0, StateObject.BUFFER_SIZE, 0, tempRemoteEP, New AsyncCallback(AddressOf Async_Send_Receive.ReceiveFrom_Callback), so) Else If so.sb.Length > 1 Then 'All the data has been read, so displays it to the console. Dim strContent As String strContent = so.sb.ToString() Console.WriteLine([String].Format("Read {0} byte from socket" + "data = {1} ", strContent.Length, strContent)) End If s.Close() End If End Sub 'ReceiveFrom_Callback
StateObject so = (StateObject) ar.AsyncState; Socket s = so.workSocket; // Creates a temporary EndPoint to pass to EndReceiveFrom. IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint tempRemoteEP = (EndPoint)sender; int read = s.EndReceiveFrom(ar, ref tempRemoteEP); if (read > 0) { so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read)); s.BeginReceiveFrom(so.buffer, 0, StateObject.BUFFER_SIZE, 0, ref tempRemoteEP , new AsyncCallback(Async_Send_Receive.ReceiveFrom_Callback), so); } else{ if (so.sb.Length > 1) { //All the data has been read, so displays it to the console. string strContent; strContent = so.sb.ToString(); Console.WriteLine(String.Format("Read {0} byte from socket" + "data = {1} ", strContent.Length, strContent)); } s.Close(); }
StateObject^ so = safe_cast<StateObject^>(ar->AsyncState); Socket^ s = so->workSocket; // Creates a temporary EndPoint to pass to EndReceiveFrom. IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 ); EndPoint^ tempRemoteEP = safe_cast<EndPoint^>(sender); int read = s->EndReceiveFrom( ar, tempRemoteEP ); if ( read > 0 ) { so->sb->Append( Encoding::ASCII->GetString( so->buffer, 0, read ) ); s->BeginReceiveFrom( so->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None , tempRemoteEP, gcnew AsyncCallback( &Async_Send_Receive::ReceiveFrom_Callback ), so ); } else { if ( so->sb->Length > 1 ) { //All the data has been read, so displays it to the console. String^ strContent = so->sb->ToString(); Console::WriteLine( "Read {0} byte from socket" + " data = {1}", strContent->Length, strContent ); } s->Close(); }
StateObject so = (StateObject)ar.get_AsyncState(); Socket s = so.workSocket; // Creates a temporary EndPoint to pass to EndReceiveFrom. IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint tempRemoteEP = (EndPoint)sender; int read = s.EndReceiveFrom(ar, tempRemoteEP); if (read > 0) { so.sb.Append(Encoding.get_ASCII().GetString(so.buffer, 0, read)); s.BeginReceiveFrom(so.buffer, 0, StateObject.BUFFER_SIZE, (SocketFlags)0, tempRemoteEP, new AsyncCallback(Async_Send_Receive.ReceiveFrom_Callback), so); } else { if (so.sb.get_Length() > 1) { //All the data has been read, so displays it to the console. String strContent; strContent = so.sb.ToString(); Console.WriteLine(String.Format("Read {0} byte from socket" + "data = {1} ", (Int32)(strContent.get_Length()), strContent)); } s.Close(); }

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


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

- Socket.EndReceiveFrom メソッドのページへのリンク