Socket.EndReceiveとは? わかりやすく解説

Socket.EndReceive メソッド (IAsyncResult, SocketError)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

保留中の非同期読み込み終了します

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Function EndReceive ( _
    asyncResult As IAsyncResult, _
    <OutAttribute> ByRef errorCode As
 SocketError _
) As Integer
Dim instance As Socket
Dim asyncResult As IAsyncResult
Dim errorCode As SocketError
Dim returnValue As Integer

returnValue = instance.EndReceive(asyncResult, errorCode)
public int EndReceive (
    IAsyncResult asyncResult,
    out SocketError errorCode
)
public:
int EndReceive (
    IAsyncResult^ asyncResult, 
    [OutAttribute] SocketError% errorCode
)
public int EndReceive (
    IAsyncResult asyncResult, 
    /** @attribute OutAttribute() */ /** @ref */ SocketError errorCode
)
JScript では、値型引数参照渡しされません。

パラメータ

asyncResult

この非同期操作の状態情報およびユーザー定義データ格納する IAsyncResult。

errorCode

ソケット エラー格納する SocketError オブジェクト

戻り値
受信したバイト数。

例外例外
例外種類条件

ArgumentNullException

asyncResultnull 参照 (Visual Basic では Nothing) です。

ArgumentException

BeginReceive メソッドへの呼び出しasyncResult返されませんでした

InvalidOperationException

EndReceive が、非同期読み取りのために以前呼び出されています。

SocketException

ソケットへのアクセス試みているときにエラー発生しました詳細については「解説」を参照してください

ObjectDisposedException

Socket閉じられています。

解説解説

EndReceive メソッドは、BeginReceive メソッド開始され非同期読み取り操作完了します

BeginReceive呼び出す前に、AsyncCallback デリゲート実装するコールバック メソッド作成する必要があります。このコールバック メソッド個別スレッド実行されBeginReceive終了時呼び出されます。コールバック メソッドは、BeginReceive メソッドからパラメータとして返されIAsyncResult受け取る必要があります

コールバック メソッド内では、IAsyncResult の AsyncState メソッド呼び出してBeginReceive渡された状態オブジェクト取得します。この状態オブジェクトから受信した Socket抽出してくださいSocket取得したら、EndReceive メソッド呼び出して読み取り操作正常に完了し読み取られたバイト数を返すことができます

EndReceive メソッドは、データ使用可能になるまでブロックしますコネクションレスプロトコル使用している場合EndReceive は、受信ネットワーク バッファ内で使用できる最初にキュー格納されデータグラム読み取ります。コネクション指向プロトコル使用している場合EndReceive メソッドは、BeginReceive メソッドsize パラメータ指定したバイト数までの、使用可能なデータをすべて読み取ります。リモート ホストShutdown メソッドSocket 接続シャットダウンし、使用できるデータがすべて受信されると、EndReceive メソッドはすぐに完了して、0 バイト返します

受信したデータ取得するには、IAsyncResultAsyncState メソッド呼び出して結果として得られる状態オブジェクト内のバッファ抽出します。

メモメモ

SocketException発生した場合は、SocketException.ErrorCode プロパティ使用して具体的なエラー コード取得してください。このコード取得したら、Windows Socket Version 2 API エラー コードドキュメントエラー詳細情報確認してください。これは MSDN ライブラリから入手できます

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Socket クラス
Socket メンバ
System.Net.Sockets 名前空間
BeginReceive
AsyncCallback
IAsyncResult
AsyncState
Shutdown

Socket.EndReceive メソッド

保留中の非同期読み込み終了します
オーバーロードの一覧オーバーロードの一覧

名前 説明
Socket.EndReceive (IAsyncResult) 保留中の非同期読み込み終了します

.NET Compact Framework によってサポートされています。

Socket.EndReceive (IAsyncResult, SocketError) 保留中の非同期読み込み終了します
参照参照

関連項目

Socket クラス
Socket メンバ
System.Net.Sockets 名前空間
BeginReceive
AsyncCallback
IAsyncResult
AsyncState
Shutdown

Socket.EndReceive メソッド (IAsyncResult)

保留中の非同期読み込み終了します

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Function EndReceive ( _
    asyncResult As IAsyncResult _
) As Integer
Dim instance As Socket
Dim asyncResult As IAsyncResult
Dim returnValue As Integer

returnValue = instance.EndReceive(asyncResult)
public int EndReceive (
    IAsyncResult asyncResult
)
public:
int EndReceive (
    IAsyncResult^ asyncResult
)
public int EndReceive (
    IAsyncResult asyncResult
)
public function EndReceive (
    asyncResult : IAsyncResult
) : int

パラメータ

asyncResult

この非同期操作の状態情報およびユーザー定義データ格納する IAsyncResult。

戻り値
受信したバイト数。

例外例外
例外種類条件

ArgumentNullException

asyncResultnull 参照 (Visual Basic では Nothing) です。

ArgumentException

BeginReceive メソッドへの呼び出しasyncResult返されませんでした

InvalidOperationException

EndReceive が、非同期読み取りのために以前呼び出されています。

SocketException

ソケットへのアクセス試みているときにエラー発生しました詳細については「解説」を参照してください

ObjectDisposedException

Socket閉じられています。

解説解説

EndReceive メソッドは、BeginReceive メソッド開始され非同期読み取り操作完了します

BeginReceive呼び出す前に、AsyncCallback デリゲート実装するコールバック メソッド作成する必要があります。このコールバック メソッド個別スレッド実行されBeginReceive終了時呼び出されます。コールバック メソッドは、BeginReceive メソッドからパラメータとして返されIAsyncResult受け取る必要があります

コールバック メソッド内では、IAsyncResult の AsyncState メソッド呼び出してBeginReceive渡された状態オブジェクト取得します。この状態オブジェクトから受信した Socket抽出してくださいSocket取得したら、EndReceive メソッド呼び出して読み取り操作正常に完了し読み取られたバイト数を返すことができます

EndReceive メソッドは、データ使用可能になるまでブロックしますコネクションレスプロトコル使用している場合EndReceive は、受信ネットワーク バッファ内で使用できる最初にキュー格納されデータグラム読み取ります。コネクション指向プロトコル使用している場合EndReceive メソッドは、BeginReceive メソッドsize パラメータ指定したバイト数までの、使用可能なデータをすべて読み取ります。リモート ホストShutdown メソッドSocket 接続シャットダウンし、使用できるデータがすべて受信されると、EndReceive メソッドはすぐに完了して、0 バイト返します

受信したデータ取得するには、IAsyncResultAsyncState メソッド呼び出して結果として得られる状態オブジェクト内のバッファ抽出します。

保留中の BeginReceive をキャンセルするには、Close メソッド呼び出します。

メモメモ

SocketException発生した場合は、SocketException.ErrorCode プロパティ使用して具体的なエラー コード取得してください。このコード取得したら、Windows Socket Version 2 API エラー コードドキュメントエラー詳細情報確認してください。これは MSDN ライブラリから入手できます

使用例使用例

保留中の非同期読み込み終了するコード例次に示しますソケット使用した非同期通信を示すコード例全体については、「ソケットコード例」を参照してください

Public Shared Sub Read_Callback(ar
 As IAsyncResult)
   Dim so As StateObject = CType(ar.AsyncState,
 StateObject)
   Dim s As Socket = so.workSocket
   
   Dim read As Integer =
 s.EndReceive(ar)
   
   If read > 0 Then
      so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read))
      s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0, New
 AsyncCallback(AddressOf Async_Send_Receive.Read_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 'Read_Callback
public static void Read_Callback(IAsyncResult
 ar){
    StateObject so = (StateObject) ar.AsyncState;
    Socket s = so.workSocket;

    int read = s.EndReceive(ar);

    if (read > 0) {
            so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
            s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0, 
                                     new AsyncCallback(Async_Send_Receive.Read_Callback),
 so);
    }
    else{
         if (so.sb.Length > 1) {
              //All of 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();
    }
}
static void Read_Callback( IAsyncResult^ ar
 )
{
   StateObject^ so = safe_cast<StateObject^>(ar->AsyncState);
   Socket^ s = so->workSocket;

   int read = s->EndReceive( ar );

   if ( read > 0 )
   {
      so->sb->Append( Encoding::ASCII->GetString( so->buffer, 0, read
 ) );
      s->BeginReceive( so->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None
,
         gcnew AsyncCallback( &Async_Send_Receive::Read_Callback ), so );
   }
   else
   {
      if ( so->sb->Length > 1 )
      {
         //All of the data has been read, so displays it to the console
         String^ strContent = so->sb->ToString();
         Console::WriteLine( String::Format( "Read {0} byte from socket"
 +
            " data = {1} ", strContent->Length, strContent ) );
      }
      s->Close();
   }
}
StateObject so = (StateObject)ar.get_AsyncState();
Socket s = so.workSocket;
int read = s.EndReceive(ar);

if (read > 0) {
    so.sb.Append(Encoding.get_ASCII().GetString(so.buffer, 0, read));
    s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE,
        (SocketFlags)0, new AsyncCallback(Async_Send_Receive.
        Read_Callback), so);
}
else {
    if (so.sb.get_Length() > 1) {
        //All of 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();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Socket クラス
Socket メンバ
System.Net.Sockets 名前空間
BeginReceive
AsyncCallback
IAsyncResult
AsyncState
Shutdown


このページでは「.NET Framework クラス ライブラリ リファレンス」からSocket.EndReceiveを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSocket.EndReceiveを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSocket.EndReceive を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Socket.EndReceive」の関連用語

Socket.EndReceiveのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Socket.EndReceiveのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS