处理异步读取的结束。Handles the end of an asynchronous read.
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
参数
An IAsyncResult that represents an asynchronous call.
返回
The number of bytes read from the NetworkStream.
例外
asyncResult 参数为 null。The asyncResult parameter is null.
已关闭基础 Socket。The underlying Socket is closed.
- 或 --or-
访问套接字时出错。An error occurred when accessing the socket.
示例
在下面的代码示例中, myReadCallback 将 BeginRead 作为回调方法提供给。In the following code example, myReadCallback is provided to BeginRead as the callback method. EndRead 在中实现, myReadCallback 以完成由启动的异步读取调用 BeginRead 。EndRead is implemented in myReadCallback to complete the asynchronous read call started by BeginRead.
// Example of EndRead, DataAvailable and BeginRead.
static void myReadCallBack( IAsyncResult^ ar )
{
NetworkStream^ myNetworkStream = safe_cast(ar->AsyncState);
array^myReadBuffer = gcnew array(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 。The EndRead method completes the asynchronous read operation started in the BeginRead method.
在调用之前 BeginRead ,需要创建一个实现委托的回调方法 AsyncCallback 。Before calling BeginRead, you need to create a callback method that implements the AsyncCallback delegate. 此回调方法在单独的线程中执行,并在返回后由系统调用 BeginRead 。This callback method executes in a separate thread and is called by the system after BeginRead returns. The callback method must accept the IAsyncResult returned from the BeginRead method as a parameter.
Within the callback method, call the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginRead method. Extract the receiving NetworkStream from this state object. 获取后 NetworkStream ,调用 EndRead 方法以成功完成读取操作并返回读取的字节数。After obtaining the NetworkStream, call the EndRead method to successfully complete the read operation and return the number of bytes read.
EndRead方法会一直阻止,直到有可用的数据。The EndRead method blocks until data is available. EndRead方法可读取尽可能多的数据,最多可达在方法的参数中指定的字节数 size BeginRead 。The EndRead method reads as much data as is available up to the number of bytes specified in the size parameter of the BeginRead method. 如果远程主机关闭 Socket 连接并且接收到所有可用数据,则该 EndRead 方法会立即完成并返回零字节。If the remote host shuts down the Socket connection and all available data has been received, the EndRead method completes immediately and returns zero bytes.
若要获取收到的数据,请调用 AsyncState 的属性 IAsyncResult ,然后提取所产生状态对象中包含的缓冲区。To obtain the received data, call the AsyncState property of the IAsyncResult, and extract the buffer contained in the resulting state object.
备注
If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. 如果是这样,请使用 ErrorCode 属性获取特定的错误代码,并参考 Windows 套接字版本 2 API 错误代码 文档以获取错误的详细说明。If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.
适用于
另请参阅