NetworkStream.EndRead(IAsyncResult) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
處理非同步讀取的結束。
public:
override int EndRead(IAsyncResult ^ asyncResult);
public override int EndRead (IAsyncResult asyncResult);
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer
參數
- asyncResult
- IAsyncResult
IAsyncResult,表示非同步呼叫。
傳回
從 NetworkStream 讀取的位元組數。
例外狀況
asyncResult
參數為 null
。
範例
在下列程式碼範例中, myReadCallback
會提供 作為 BeginRead 回呼方法。 EndRead 會在 中 myReadCallback
實作 ,以完成 由 BeginRead 啟動的非同步讀取呼叫。
// Example of EndRead, DataAvailable and BeginRead.
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 );
}
// Example of EndRead, DataAvailable and BeginRead.
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);
}
' Example of EndRead, DataAvailable and BeginRead.
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
備註
方法 EndRead 會完成 方法中啟動的 BeginRead 非同步讀取作業。
呼叫 BeginRead 之前,您必須建立實作委派的 AsyncCallback 回呼方法。 這個回呼方法會在個別的執行緒中執行,並在傳回之後 BeginRead 由系統呼叫。 回呼方法必須接受 IAsyncResult 從 方法傳回的 BeginRead 做為參數。
在回呼方法內,呼叫 AsyncState 的 IAsyncResult 屬性,以取得傳遞至 BeginRead 方法的狀態物件。 從這個狀態物件擷取 NetworkStream 接收。 取得 NetworkStream 之後,請呼叫 EndRead 方法以成功完成讀取作業,並傳回讀取的位元組數目。
方法會 EndRead 封鎖直到資料可用為止。 方法 EndRead 會讀取的資料量,上限為 方法的 參數 BeginRead 中指定的 size
位元組數目。 如果遠端主機關閉 Socket 連線,而且已收到所有可用的資料,則 EndRead 方法會立即完成並傳回零個位元組。
若要取得接收的資料,請呼叫 AsyncState 的 IAsyncResult 屬性,並擷取結果狀態物件中包含的緩衝區。
注意
如果您收到 IOException ,請檢查 InnerException 屬性,以判斷它是否由 SocketException 所造成。 如果是,請使用 ErrorCode 屬性來取得特定錯誤碼,並參閱Windows Sockets 第 2 版 API 錯誤碼檔,以取得錯誤的詳細描述。