C# 异步SOCKET问题
异步接收函数如下:private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
try
{
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
GetDataEvent(state.buffer, bytesRead, handler.Handle);
//handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
// new AsyncCallback(ReadCallback), state);
}
catch (Exception ex)
{ }
}
}
异步发送函数如下:
private void SendCallback(IAsyncResult ar)
{
Socket handler = null;
try
{
handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
catch (Exception e)
{ }
}
StateObject结构如下:
public class StateObject
{
//Client socket.
public Socket workSocket = null;
//Size of receive buffer.
public const int BufferSize = 1500;
//Receive buffer.
public byte[] buffer = new byte[BufferSize];
}
我现在要用SOCKET传输文件,传输方式是,客户端发一次请求数据,服务器返回一次数据,然后客户端再发送数据,服务器再返回一次数据,一直到文件传输完。在这个过程中,如果在ReadCallback和SendCallback中同时调用BeginReceive方法,则在传输过程中内在会一直增大;如果去掉ReadCallback中的BeginReceive调用,不调用SendCallback函数就收不到后续的数据;如果去掉SendCallback中的Beginreceive调用,只保留ReadCallback中的调用,则在调用SendCallback后就不能接收数据,请问这个问题要怎么处理?望高手指点!
补充:.NET技术 , C#