日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#endread怎么打印出来_NetworkStream.EndRead(IAsyncResult) 方法 (System.Net.Sockets) | Microsoft Docs...

發布時間:2024/7/23 C# 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#endread怎么打印出来_NetworkStream.EndRead(IAsyncResult) 方法 (System.Net.Sockets) | Microsoft Docs... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

處理異步讀取的結束。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.

適用于

另請參閱

總結

以上是生活随笔為你收集整理的c#endread怎么打印出来_NetworkStream.EndRead(IAsyncResult) 方法 (System.Net.Sockets) | Microsoft Docs...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。