C#socket.Receive方法socketError10035 WouldBlock处理

      在服务器端为NIO时候,实现长连接,c#为客户端时候,循环调用socket.Receive(循环读取等待服务端消息),会出现 ErrorCode: 0x00002733(10035 WouldBlock )无法立即完成一个非阻止性套接字操作.(A non-blocking socket operation could not be completed immediately.) ,之后设置 socketc.Blocking = true; ,仍然没用,没办法,只能查阅MSDN,终于找到了一段话

                The Connect method will block, unless you specifically set the Blocking property to false prior to calling Connect. If you are using a connection-oriented protocol like TCP and you do disable blocking, Connect will throw a SocketException because it needs time to make the connection. Connectionless protocols will not throw an exception because they simply establish a default remote host. You can use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error. If the error returned WSAEWOULDBLOCK, the remote host connection has been initiated by a connection-oriented Socket, but has not yet completed successfully. Use the Poll method to determine when the Socket is finished connecting. ,

             里面提示,远程主机未完成,造成的一个错误,需要,用Poll方法,判断是否完成,

           public bool Poll ( int microSeconds,SelectMode mode)  ,Poll 方法将会检查 Socket 的状态。指定 selectMode 参数的 SelectMode.SelectRead,可确定 Socket 是否为可读。指定 SelectMode.SelectWrite,可确定 Socket 是否为可写。使用 SelectMode.SelectError 检测错误条件。Poll 将在指定的时段(以 microseconds 为单位)内阻止执行。如果希望无限期的等待响应,则将 microSeconds 设置为一个负整数。

          修改代码如下,OK.

                       if (socketc.Poll(-1, SelectMode.SelectRead))
                         {
                           rei = socket.Receive(hbs);  
                         }

         

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值