怎么强行停止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,因为我还要继续发送新的命令来得到接收数据)。。。
谢谢了,前面发的一帖没描述好,被忽视了。。。。
{
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,因为我还要继续发送新的命令来得到接收数据)。。。
谢谢了,前面发的一帖没描述好,被忽视了。。。。
...全文
相关推荐
TCP转串口(socket转SerialPort)
一款TCP转串口的转发工具,本人亲自开发,提学习和交流。 程序将来自TCP服务端发送过来的socket数据转到串口发送出去,架起了TCP与串口之前的通讯, 程序测试稳定可靠。使用vs2017开发。
基于TCP异步Socket模型的介绍
本篇文章小编将为大家介绍,基于TCP异步Socket模型的介绍,需要的朋友参考下
TCP与串口间的通信方式。Socket通信
Socket通信的同步、异步方法。串口到客户端的通信、客户端到服务端的通信的多种方法。
c# socket 传输文件
c# socket 传输文件,c# socket 传输文件,c# socket 传输文件
一个基于VB.net的异步Socket网络TCP通信
一个基于VB.net的异步Socket网络TCP通信


