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

Public Overrides Function BeginRead ( _ buffer As Byte(), _ offset As Integer, _ size As Integer, _ callback As AsyncCallback, _ state As Object _ ) As IAsyncResult
Dim instance As NetworkStream Dim buffer As Byte() Dim offset As Integer Dim size As Integer Dim callback As AsyncCallback Dim state As Object Dim returnValue As IAsyncResult returnValue = instance.BeginRead(buffer, offset, size, callback, state)
public override IAsyncResult BeginRead ( byte[] buffer, int offset, int size, AsyncCallback callback, Object state )
public: virtual IAsyncResult^ BeginRead ( array<unsigned char>^ buffer, int offset, int size, AsyncCallback^ callback, Object^ state ) override
public IAsyncResult BeginRead ( byte[] buffer, int offset, int size, AsyncCallback callback, Object state )
public override function BeginRead ( buffer : byte[], offset : int, size : int, callback : AsyncCallback, state : Object ) : IAsyncResult
戻り値
非同期呼び出しを表す IAsyncResult。


BeginRead メソッドは受信ネットワーク バッファからの非同期のデータ読み取りを開始します。BeginRead メソッドを呼び出すと、個別の実行スレッド内でデータを受信できます。
AsyncCallback デリゲートを実装するコールバック メソッドを作成し、その名前を BeginRead メソッドに渡す必要があります。使用する state パラメータは、少なくとも NetworkStream を格納している必要があります。コールバック メソッド内で受信データを取得しなければならないため、読み取りバッファなどの有用な情報を保持するための小さなクラスまたは構造体を作成する必要があります。構造体またはクラスのインスタンスは、state パラメータを使用して BeginRead メソッドに渡します。
コールバック メソッドは EndRead メソッドを呼び出す必要があります。アプリケーションが BeginRead を呼び出すと、データが受信されるかエラーが発生するまで待機した後に、指定したコールバック メソッドが個別のスレッドを使用して実行され、EndRead は指定した NetworkStream がデータを読み取るか例外をスローするまでブロックします。BeginRead メソッドを呼び出してから元のスレッドをブロックする場合は、WaitOne メソッドを使用します。元のスレッドの実行を継続させるには、コールバック メソッドで Set を呼び出します。コールバック メソッドの記述に関する追加情報については、「Callback のサンプル」を参照してください。
BeginRead メソッドは、size パラメータで指定したバイト数までの、使用可能なデータをすべて読み取ります。
![]() |
---|
IOException が発生した場合は、InnerException プロパティをチェックして、この原因が SocketException かどうかを確認してください。その場合、ErrorCode プロパティを使用して特定のエラー コードを取得してください。エラーの詳細については、MSDN で Windows Socket Version 2 API のエラー コードのドキュメントを参照してください。 |

BeginRead を使用して、ネットワーク ストリームから非同期的にデータを読み取るコード例を次に示します。myReadCallBack メソッドは AsyncCallback デリゲートを実装しており、BeginRead の終了時に呼び出されます。
' Check to see if this NetworkStream is readable. If myNetworkStream.CanRead Then Dim myReadBuffer(1024) As Byte myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, New AsyncCallback(AddressOf NetworkStream_ASync_Send_Receive.myReadCallBack), myNetworkStream) allDone.WaitOne() Else Console.WriteLine("Sorry. You cannot read from this NetworkStream.") End If
// Check to see if this NetworkStream is readable. if(myNetworkStream.CanRead){ byte[] myReadBuffer = new byte[1024]; myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack), myNetworkStream); allDone.WaitOne(); } else{ Console.WriteLine("Sorry. You cannot read from this NetworkStream."); }
// Check to see if this NetworkStream is readable. if ( myNetworkStream->CanRead ) { array<Byte>^myReadBuffer = gcnew array<Byte>(1024); myNetworkStream->BeginRead( myReadBuffer, 0, myReadBuffer->Length, gcnew AsyncCallback( &MyNetworkStreamClass::myReadCallBack ), myNetworkStream ); allDone->WaitOne(); } else { Console::WriteLine( "Sorry. You cannot read from this NetworkStream." ); }
// Check to see if this NetworkStream is readable. if (myNetworkStream.get_CanRead()) { ubyte myReadBuffer[] = new ubyte[1024]; myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.get_Length(), new AsyncCallback(NetworkStreamASyncSendReceive. MyReadCallBack), myNetworkStream); allDone.WaitOne(); } else { Console.WriteLine("Sorry. You cannot read from this " + "NetworkStream."); }

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.BeginRead メソッドを検索する場合は、下記のリンクをクリックしてください。

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