Ansychronous Socket Client ReceiveCallback not executing



嗨,我正在使用以下示例代码开发 C# Winforms 应用程序:

https://msdn.microsoft.com/en-us/library/bbx2eya8(v=vs.110).aspx

它连接到正确的IP地址和端口号,但在发送字符串命令并等待响应后,应用程序会卡住,因为ReceiveCallback未执行,因此未设置receiveDone ManualResetEvent。我 100% 确定我应该收到响应,但应用程序只是卡住了并且不执行 ReceiveCallback 方法。ReceiveCallback 方法未执行的原因可能是什么?

private static void Receive(Socket client)
    {
        try
        {
            // Create the state object.
            StateObject state = new StateObject();
            state.workSocket = client;
            // Begin receiving the data from the remote device.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
 private static void ReceiveCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the state object and the client socket 
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket client = state.workSocket;
            // Read data from the remote device.
            int bytesRead = client.EndReceive(ar);
            if (bytesRead > 0)
            {
                // There might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                // Get the rest of the data.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback), state);
            }
            else {
                // All the data has arrived; put it in response.
                if (state.sb.Length > 1)
                {
                    response = state.sb.ToString();
                }
                // Signal that all bytes have been received.
                receiveDone.Set();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

上下文:该应用程序是直流电动扭矩工具控制器的接口。我连接到控制器 IP 地址和端口 4545,然后我能够发送开放协议命令来更改设置。

解决方案可在

https://social.msdn.microsoft.com/Forums/vstudio/en-US/05dcda20-06f9-419b-bfc0-bcb8cfeb3693/socket-receivecallback-not-sending-notification-when-buffer-has-emptied?forum=csharpgeneral 中找到

这是我的修复代码。请检查"添加到修复"部分。

try
        {
            // Retrieve the state object and the client socket   
            // from the asynchronous state object.  
            StateObject state = (StateObject)ar.AsyncState;
            Socket client = state.workSocket;
            // Read data from the remote device.  
            int bytesRead = client.EndReceive(ar);
            if (bytesRead > 0)
            {
                // There might be more data, so store the data received so far.  
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
            }                                                       // Add to fix
                                                                    // Add to fix
            int bytesRemain = state.workSocket.Available;           // Add to fix
            if (bytesRemain>0)                                      // Add to fix
            {                                                       // Add to fix
                //  Get the rest of the data.  
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None,
                    new AsyncCallback(ReceiveCallback), state);
            }
            else
            {
                // All the data has arrived; put it in response.  
                if (state.sb.Length >= 1)                 //<-- Fixed from>1 to >=1
                    response = state.sb.ToString();
                else
                    response = ""; //OR null;
                // Signal that all bytes have been received.  
                receiveDone.Set();
            }
        }

以下是我对为什么原始不能工作的分析:

  • 我向服务器发送消息,例如"*IDN?服务器回复了一条包含 34 个字母的消息,例如"Instrument ABC,xx,xx,xx"。它已被 ReceiveCallBack 成功读取。所以字节读取 = 34。
  • 但是有一个检查说,如果bytesRead>0,可能是,只是可能那里还有数据,所以ReceiveCallBack本身启动了一个新客户端。再次开始接收。
  • 但是这一次,没有来自服务器的数据。 所以客户端等了又等,再等了又等,接收回调再也没有被回调......
  • 因此,ReceiveCallBack 的 else-branch 永远不会被执行。因此,"响应"变量从未设置,接收也从未被设置。
我会

将其添加为评论,但我没有声誉。

  1. 你从哪里打电话给这个?您应该有一个循环,一旦连接到套接字,它就会调用接收。你能给我们看一下调用接收的代码吗?

  2. StateObject.BufferSize 的值是多少?

  3. 是否引发任何异常?

最新更新