【发布时间】:2012-02-21 18:29:32
【问题描述】:
我有一个异步读取方法...
private void read(IAsyncResult ar) {
//Get the Server State Object
ServerState state = (ServerState)ar.AsyncState;
//read from the socket
int readCount = state.socket.EndReceive(ar);
//check if reading is done, move on if so, trigger another read if not
if (readCount > 0) {
//purge the buffer and start another read
state.purgeBuffer();
state.socket.BeginReceive(state.buffer, 0, ServerState.bufferSize, 0, new AsyncCallback(read), state);
}
else {
//all bytes have been read, dispatch the message
dispatch(state);
}
}
我遇到的问题是,如果连接关闭,read 仅为 0。怎么说呢,这是该消息的结尾并将数据传递给调度程序,同时保持套接字打开以接受新消息。
谢谢!
【问题讨论】:
标签: c# sockets networking asynchronous