EndReceive  接受数据的问题

zhao5195 2008-08-27 12:37:10
public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
Log("第一步");
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;

try
{
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);





EndReceive 接收到的数据是是否每次需要达到state.buffer 在去接受呢?还是可以接受一点点用回调再去接受呢 ?????
...全文
232 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hundredpig 2009-06-22
  • 打赏
  • 举报
回复
如果您使用的是面向连接的协议,则 EndReceive 方法将读取所有可用的数据,直到达到 BeginReceive 方法的 size 参数所指定的字节数为止。如果远程主机使用 Shutdown 方法关闭了 Socket 连接,并且所有可用数据均已收到,则 EndReceive 方法将立即完成并返回零字节。
如果数据量大于size,那么就要多次执行EndReceive 方法了。直到读完为止。
aaajedll 2008-08-27
  • 打赏
  • 举报
回复
Study

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧