怎么强行停止BEGINRECEIVE过程(不是关闭SOCKET哦)

juventusland juventusland 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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,因为我还要继续发送新的命令来得到接收数据)。。。
谢谢了,前面发的一帖没描述好,被忽视了。。。。
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
juventusland
C# 111,129 成员 642,540 内容