怎么强行停止BEGINRECEIVE过程(不是关闭SOCKET哦)
private void ReceiveA(Socket client)
{
try
{
StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveACallback), state);
}
catch (Exception e)
{
}
}
private void ReceiveACallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
int bytesRead = client.EndReceive(ar);
//here, client has some logic problem, it keeps receiving
if (bytesRead > 0)
{
response = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
state.sb.Append(response);//ascii string get append
if (response.IndexOf("val")> -1 || response.IndexOf("\r\n> ") > -1)
{
if (state.sb.Length > 1) //get ascii code out true
{
response = state.sb.ToString();
state.sb.Clear();
}
receiveDone.Set();
}
else
{
// Get more data
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveACallback), state);
}
}
}
catch{}
}
可以看到在RECEIVEACALLBACK中,由于一次接受数据不够,利用ELSE称述中的CLIENT.BEGINRECEIVE继续接受数据。但是我不想让这一过程一直接收下去,请问有什么办法强行停止吗?(不能关闭SOCKET,因为我还要继续发送新的命令来得到接收数据)。。。
谢谢了,前面发的一帖没描述好,被忽视了。。。。