NetworkStream.EndRead メソッド
名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文
Dim instance As NetworkStream Dim asyncResult As IAsyncResult Dim returnValue As Integer returnValue = instance.EndRead(asyncResult)


EndRead メソッドは、BeginRead メソッドで開始された非同期の読み取り操作を完了します。
BeginRead を呼び出す前に、AsyncCallback デリゲートを実装するコールバック メソッドを作成する必要があります。このコールバック メソッドは個別のスレッドで実行され、BeginRead の終了時に呼び出されます。コールバック メソッドは、BeginRead メソッドからパラメータとして返された IAsyncResult を受け取る必要があります。
コールバック メソッド内では、IAsyncResult の AsyncState プロパティを呼び出して、BeginRead に渡された状態オブジェクトを取得します。この状態オブジェクトから受信した NetworkStream を抽出してください。NetworkStream を取得したら、EndRead メソッドを呼び出して読み取り操作を正常に完了し、読み取られたバイト数を返します。
EndRead メソッドは、データが使用可能になるまでブロックします。EndRead メソッドは、BeginRead メソッドの size パラメータで指定されているバイト数までの、使用可能なデータをすべて読み取ります。リモート ホストが Socket 接続をシャットダウンし、使用できるデータがすべて受信されると、EndRead メソッドはすぐに完了して、0 バイトを返します。
受信したデータを取得するには、IAsyncResult の AsyncState プロパティを呼び出して、結果として得られる状態オブジェクト内のバッファを抽出します。
![]() |
---|
IOException が発生した場合は、InnerException プロパティをチェックして、この原因が SocketException かどうかを確認してください。その場合、ErrorCode プロパティを使用して特定のエラー コードを取得してください。エラーの詳細については、MSDN で Windows Socket Version 2 API のエラー コードのドキュメントを参照してください。 |

myReadCallback をコールバック メソッドとして BeginRead に提供し、BeginRead で開始される非同期読み取りの呼び出しを完了するために、その myReadCallback に EndRead を実装するコード例を次に示します。
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

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に収録されているすべての辞書からNetworkStream.EndRead メソッドを検索する場合は、下記のリンクをクリックしてください。

- NetworkStream.EndRead メソッドのページへのリンク