为了账号安全,请及时绑定邮箱和手机立即绑定

各位大佬门我想问一下,对于异步,如何可以在Receive方法中得到ReceiveCallBack()中 int bytesRead = client.EndReceive(ar); bytesRead值?

各位大佬门我想问一下,对于异步,如何可以在Receive方法中得到ReceiveCallBack()中 int bytesRead = client.EndReceive(ar); bytesRead值?

C#
长风秋雁 2018-12-06 21:45:30
private static void Receive(Socket client) { try { // Create the state object. StateObject state = new StateObject(); state.workSocket = client; // Begin receiving the data from the remote device. client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void ReceiveCallback( IAsyncResult ar ) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject) ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead)); // Get the rest of the data. client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state); } else { // All the data has arrived; put it in response. if (state.sb.Length > 1) { response = state.sb.ToString(); } // Signal that all bytes have been received. receiveDone.Set(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

ar.asyncResult

 

你要的东西在这个变量里

查看完整回答
反对 回复 2018-12-24
  • 1 回答
  • 0 关注
  • 535 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信