admin管理员组文章数量:1619291
在服务器端为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);
}
本文标签: 方法ReceiveSocketWouldBlock
版权声明:本文标题:C#socket.Receive方法socketError10035 WouldBlock处理 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1725429890a1022786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论