如何在C#中进行非阻塞套接字调用以确定连接状态?

如何在C#中进行非阻塞套接字调用以确定连接状态?,第1张

概述Socket上Connected属性的MSDN文档说明如下: The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, m Socket上Connected属性的MSDN文档说明如下:

The value of the Connected property
reflects the state of the connection
as of the most recent operation. If
you need to determine the current
state of the connection,make a
nonblocking,zero-byte Send call. If
the call returns successfully or
throws a WAEWOulDBLOCK error code
(10035),then the socket is still
connected; otherwise,the socket is no
longer connected.

我需要确定连接的当前状态 – 如何进行非阻塞,零字节发送调用?

解决方法 Socket.Connected属性(至少.NET 3.5版本)的MSDN文档底部的示例显示了如何执行此 *** 作:
// .Connect throws an exception if unsuccessfulclIEnt.Connect(anEndPoint);// This is how you can determine whether a socket is still connected.bool blockingState = clIEnt.Blocking;try{    byte [] tmp = new byte[1];    clIEnt.Blocking = false;    clIEnt.Send(tmp,0);    Console.Writeline("Connected!");}catch (SocketException e) {    // 10035 == WSAEWOulDBLOCK    if (e.NativeErrorCode.Equals(10035))        Console.Writeline("Still Connected,but the Send would block");    else    {        Console.Writeline("disconnected: error code {0}!",e.NativeErrorCode);    }}finally{    clIEnt.Blocking = blockingState;} Console.Writeline("Connected: {0}",clIEnt.Connected);
总结

以上是内存溢出为你收集整理的如何在C#中进行非阻塞套接字调用以确定连接状态?全部内容,希望文章能够帮你解决如何在C#中进行非阻塞套接字调用以确定连接状态?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1263533.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存