C# Socket.Receive()一直为0问题解决

最近写了个Socket服务端程序,发现有时一直收到数据长度为0,而且Receive()函数也不阻塞。代码如下。

Socket clientConnection = (Socket) clientSocket;
            while (_needReceive)
            {
                var rcvBytes = new byte[1024];
                try
                {
                    var length = clientConnection.Receive(rcvBytes);
                    var rcvStr = Encoding.UTF8.GetString(rcvBytes, 0, length);
                    ShowInfo($"接收到客户端:{rcvStr},length:{length}");
                    if (length > 0)
                    {
                        var response = "abcd";
                        var respBytes = Encoding.UTF8.GetBytes(response);
                        var ret = clientConnection.Send(respBytes);
                        ShowInfo($"发送到客户端:{response}");
                    }

                }
                catch (Exception e)
                {
                    clientConnection.Close();
                    ShowInfo($"通讯失败:{e.Message}");
                    break;
                }
            }

客户端Socket执行了Close()方法后,服务端的Receive()方法,每次都接收到0个字节,并且不阻塞。

从一本书上看到以下描述,书名为C# Network Programming,作者Richard Blum。
The return value from the Receive() method has to be examined to see if the remote client disconnected from the session. This can be determined by detecting a zero return value. If the remote client disconnected, you must close the socket …
翻译下来就是,如果Receive()方法返回0,这个可以作为客户端关闭了的标志。因此程序改为如下即可。

Socket clientConnection = (Socket) clientSocket;
            while (_needReceive)
            {
                var rcvBytes = new byte[1024];
                try
                {
                    var length = clientConnection.Receive(rcvBytes);
                    
                    if (length > 0)
                    {
                        var rcvStr = Encoding.UTF8.GetString(rcvBytes, 0, length);
                        ShowInfo($"接收到客户端:{rcvStr},length:{length}");
                        string response = "abcd";
                        var respBytes = Encoding.UTF8.GetBytes(response);
                        var ret = clientConnection.Send(respBytes);
                        ShowInfo($"发送到客户端:{response}");
                    }
                    else
                    {
                        clientConnection.Close();
                        ShowInfo($"客户端关闭。");
                        break;
                    }

                }
                catch (Exception e)
                {
                    clientConnection.Close();
                    ShowInfo($"通讯失败:{e.Message}");
                    break;
                }
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值