NetworkStream.EndRead メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > NetworkStream.EndRead メソッドの意味・解説 

NetworkStream.EndRead メソッド

非同期読み取り終了処理します

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

Public Overrides Function
 EndRead ( _
    asyncResult As IAsyncResult _
) As Integer
Dim instance As NetworkStream
Dim asyncResult As IAsyncResult
Dim returnValue As Integer

returnValue = instance.EndRead(asyncResult)
public override int EndRead (
    IAsyncResult asyncResult
)
public:
virtual int EndRead (
    IAsyncResult^ asyncResult
) override
public int EndRead (
    IAsyncResult asyncResult
)
public override function EndRead (
    asyncResult : IAsyncResult
) : int

パラメータ

asyncResult

非同期呼び出しを表す IAsyncResult。

戻り値
NetworkStream から読み取るバイト数。

例外例外
例外種類条件

ArgumentException

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

IOException

になっている Socket閉じてます。

または

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

ObjectDisposedException

NetworkStream閉じてます。

解説解説

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

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

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

EndRead メソッドは、データ使用可能になるまでブロックしますEndRead メソッドは、BeginRead メソッドsize パラメータ指定されているバイト数までの、使用可能なデータをすべて読み取ります。リモート ホストSocket 接続シャットダウンし、使用できるデータがすべて受信されると、EndRead メソッドはすぐに完了して、0 バイト返します

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

メモメモ

IOException発生した場合は、InnerException プロパティチェックして、この原因が SocketException かどうか確認してくださいその場合、ErrorCode プロパティ使用して特定のエラー コード取得してくださいエラー詳細については、MSDNWindows Socket Version 2 APIエラー コードドキュメント参照してください

使用例使用例

myReadCallbackコールバック メソッドとして BeginRead提供しBeginRead開始される非同期読み取り呼び出し完了するために、その myReadCallbackEndRead実装するコード例次に示します

Public Shared Sub myReadCallBack(ar
 As IAsyncResult)
   
   Dim myNetworkStream As NetworkStream = CType(ar.AsyncState,
 NetworkStream)
   Dim myReadBuffer(1024) As Byte
   Dim myCompleteMessage As [String] = ""
   Dim numberOfBytesRead As Integer
   
   numberOfBytesRead = myNetworkStream.EndRead(ar)
   myCompleteMessage = [String].Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer,
 0, numberOfBytesRead))
   
   ' message received may be larger than buffer size so loop through
 until you have it all.
   While myNetworkStream.DataAvailable
      
      myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, New
 AsyncCallback(AddressOf NetworkStream_ASync_Send_Receive.myReadCallBack),
 myNetworkStream)
   End While
   
   
   ' Print out the received message to the console.
   Console.WriteLine(("You received the following message : "
 + myCompleteMessage))
End Sub 'myReadCallBack

'Entry point which delegates to C-style main Private Function
Public Overloads Shared
 Sub Main()
   Main(System.Environment.GetCommandLineArgs())
End Sub


public static void myReadCallBack(IAsyncResult
 ar ){

    NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
    byte[] myReadBuffer = new byte[1024];
    String myCompleteMessage = "";
    int numberOfBytesRead;

    numberOfBytesRead = myNetworkStream.EndRead(ar);
    myCompleteMessage = 
        String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0,
 numberOfBytesRead));    
    
    // message received may be larger than buffer size so loop through
 until you have it all.
    while(myNetworkStream.DataAvailable){
        
        myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, 
                                                   new AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack),
 
                                                   myNetworkStream);  

    }

    // Print out the received message to the console.
    Console.WriteLine("You received the following message : " +
                                myCompleteMessage);
}

static void myReadCallBack( IAsyncResult^ ar
 )
{
   NetworkStream^ myNetworkStream = safe_cast<NetworkStream^>(ar->AsyncState);
   array<Byte>^myReadBuffer = gcnew array<Byte>(1024);
   String^ myCompleteMessage = "";
   int numberOfBytesRead;
   numberOfBytesRead = myNetworkStream->EndRead( ar );
   myCompleteMessage = String::Concat( myCompleteMessage, Encoding::ASCII->GetString(
 myReadBuffer, 0, numberOfBytesRead ) );
   
   // message received may be larger than buffer size so loop through
 until you have it all.
   while ( myNetworkStream->DataAvailable )
   {
      AsyncCallback^ pasync = gcnew AsyncCallback( &myReadCallBack );
      myNetworkStream->BeginRead( myReadBuffer, 0, myReadBuffer->Length, pasync,
 myNetworkStream );
   }

   // Print out the received message to the console.
   Console::WriteLine( "You received the following message : {0}", myCompleteMessage
 );
}
public static void MyReadCallBack(IAsyncResult
 ar)
{
    NetworkStream myNetworkStream = (NetworkStream)ar.get_AsyncState();
    ubyte myReadBuffer[] = new ubyte[1024];
    String myCompleteMessage = "";
    int numberOfBytesRead;

    numberOfBytesRead = myNetworkStream.EndRead(ar);
    myCompleteMessage = String.Concat(myCompleteMessage, 
        Encoding.get_ASCII().GetString(myReadBuffer, 0, numberOfBytesRead));
    // message received may be larger than buffer size so loop through
    // until you have it all.
    while (myNetworkStream.get_DataAvailable()) {
        myNetworkStream.BeginRead(myReadBuffer, 0,
            myReadBuffer.get_Length(),
            new AsyncCallback(NetworkStreamASyncSendReceive.MyReadCallBack)
,
            myNetworkStream);
    }
    // Print out the received message to the console.
    Console.WriteLine(("You received the following message : " 
        + myCompleteMessage));
} //MyReadCallBack
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
NetworkStream クラス
NetworkStream メンバ
System.Net.Sockets 名前空間
BeginRead



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

辞書ショートカット

すべての辞書の索引

NetworkStream.EndRead メソッドのお隣キーワード
検索ランキング

   

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



NetworkStream.EndRead メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS