NetworkStream.EndRead メソッド
非同期読み取りの終了を処理します。
Overrides Public Function EndRead( _
ByVal asyncResult As IAsyncResult _) As Integer
[C#]
public override int EndRead(IAsyncResultasyncResult);
[C++]
public: int EndRead(IAsyncResult* asyncResult);
[JScript]
public override function EndRead(
asyncResult : IAsyncResult) : int;
パラメータ
- asyncResult
非同期呼び出しを表す IAsyncResult 。
戻り値
NetworkStream から読み取るバイト数。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | asyncResult が null 参照 (Visual Basic では Nothing) です。 |
IOException | 基になっている Socket が閉じています。
または ネットワークの読み取り中にエラーが発生しました。 |
ObjectDisposedException | NetworkStream が閉じています。 |
IOException | ソケットへのアクセス中にエラーが発生しました。詳細については「解説」を参照してください。 |
解説
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 を使用して特定のエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのマニュアルから、エラーの詳細情報を確認できます。これは MSDN から入手できます。
使用例
[Visual Basic, C#, C++] 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
[C#]
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);
}
[C++]
static void myReadCallBack(IAsyncResult* ar) {
NetworkStream* myNetworkStream = __try_cast<NetworkStream*>(ar->AsyncState);
Byte myReadBuffer[] = new Byte[1024];
String* myCompleteMessage = S"";
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 = new AsyncCallback(0, &myReadCallBack);
myNetworkStream->BeginRead(myReadBuffer, 0, myReadBuffer->Length,
pasync,
myNetworkStream);
}
// Print out the received message to the console.
Console::WriteLine(S"You received the following message : {0}", myCompleteMessage);
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
NetworkStream クラス | NetworkStream メンバ | System.Net.Sockets 名前空間 | BeginRead | 非同期呼び出しの組み込み