【问题标题】:Ansychronous Socket Client ReceiveCallback not executing异步套接字客户端 ReceiveCallback 未执行
【发布时间】:2016-03-16 21:49:29
【问题描述】:

您好,我正在使用以下示例代码开发 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,而不是我能够发送开放协议命令来更改设置。

【问题讨论】:

    标签: c# sockets c#-4.0 tcpclient beginreceive


    【解决方案1】:

    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个字母的消息,如“仪器ABC,xx,xx,xx”。 ReceiveCallBack 已成功读取它。所以 bytesRead = 34。
    • 但是有一个检查说如果 bytesRead >0,可能是,只是可能那里还有数据,所以 ReceiveCallBack 本身启动了一个新的客户端。再次开始接收。
    • 但这一次,没有来自服务器的数据。所以客户端等待,等待,等待,ReceiveCallBack 再也没有被回调......
    • 所以 ReceiveCallBack 的 else 分支永远不会被执行。因此,“response”变量从未设置,receiveDone 也从未设置。

    【讨论】:

      【解决方案2】:

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

      1. 你从哪里打电话?一旦你连接到你的套接字,你应该有一个调用 Receive 的循环。你能告诉我们调用 Receive 的代码吗?

      2. StateObject.BufferSize 的值是多少?

      3. 是否有任何异常被抛出?

      【讨论】:

        猜你喜欢
        • 2012-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多