简体   繁体   English

验证套接字是否打开的可靠方法是什么?

[英]What's a reliable way to verify that a socket is open?

I'm developing a C# application, working with TCP sockets.我正在开发一个 C# 应用程序,使用 TCP sockets。

While debugging, I arrive in this piece of source code:在调试时,我得到了这段源代码:

if ((_socket!= null) && (_socket.Connected)) 
{
     Debug.WriteLine($"..."); <= my breakpoint is here.
     return true;
}

In my watch-window, the value of _socket.RemoteEndPoint is:在我的监视窗口中, _socket.RemoteEndPoint的值为:

_socket.RemoteEndPoint  {10.1.0.160:50001}  System.Net.EndPoint {...}

Still, in commandline, when I run netstat -aon | findstr /I "10.1.0.160"尽管如此,在命令行中,当我运行netstat -aon | findstr /I "10.1.0.160" netstat -aon | findstr /I "10.1.0.160" , I just see this: netstat -aon | findstr /I "10.1.0.160" ,我只看到这个:

TCP    10.1.13.200:62720      10.1.0.160:3389        ESTABLISHED     78792
TCP    10.1.13.200:63264      10.1.0.160:445         ESTABLISHED     4

=> the remote endpoint "10.1.0.160:50001" is not visible in netstat result. => 远程端点“10.1.0.160:50001”在netstat结果中不可见。

As netstat seems not reliable for testing TCP sockets, what tool can I use instead?由于netstat似乎无法可靠地测试 TCP sockets,我可以使用什么工具代替?

(For your information: even after having run further, there still is no .netstat" entry.) (供您参考:即使在进一步运行之后,仍然没有 .netstat" 条目。)

Documentation of Socket.Connected , says: Socket.Connected的文档说:

The value of the Connected property reflects the state of the connection as of the most recent operation. Connected 属性的值反映了最近操作时连接的 state。 If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call.如果您需要确定连接的当前 state,请进行非阻塞的零字节发送调用。 If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected;如果调用成功返回或抛出 WAEWOULDBLOCK 错误代码(10035),则套接字仍处于连接状态; otherwise, the socket is no longer connected.否则,套接字不再连接。

So if it returns true - socket has been "connected" some time in the past, but not necessary is still alive right now.因此,如果它返回 true - socket 在过去的某个时间已经“连接”,但现在还没有必要。

That's because it's not possible to detect if your TCP connection is still alive with certainly without contacting the other side in one way or another.那是因为如果你的 TCP 连接还没有以某种方式联系另一端,肯定是不可能检测到的。 The TCP connection is kind of "virtual", two sides just exchange packets but there is no hard link between them. TCP 连接有点“虚拟”,双方只是交换数据包,但它们之间没有硬链接。 When one side decides to finish communication - it sends a packet and waits for response from the other side.当一方决定完成通信时 - 它发送一个数据包并等待另一方的响应。 If all goes well two sides will both agree that connection is closed.如果一切顺利,双方都会同意关闭连接。

However, if side A does NOT send this close packet, for example because it crashed, or inte.net died and so on - the other side B has no way to figure out that connection is no longer active UNTIL it tries to send some data to A. Then this send will fail and now B knows connection is dead.但是,如果 A 方不发送此关闭数据包,例如因为它崩溃或 inte.net 死了等等 - 另一方 B 无法确定连接不再处于活动状态,直到它尝试发送一些数据到 A。然后这个发送将失败,现在 B 知道连接已经死了。

So if you really need to know if other side is still alive - then you have to send some data there.所以如果你真的需要知道另一边是否还活着——那么你必须向那里发送一些数据。 You can use keepalive which is available on TCP sockets (which basically does the same - sends some data from time to time).您可以使用在 TCP sockets 上可用的 keepalive(基本上做同样的事情 - 不时发送一些数据)。 Or if you always write on this connection first (say other side is server and you do requests to it from time to time, but do not expect any data between those requests) - then just don't check if the other side is alive - you will know that when you will attempt to write next time.或者如果你总是先写这个连接(比如另一端是服务器并且你不时向它发出请求,但不要期望这些请求之间有任何数据) - 那么就不要检查另一端是否还活着 -您会知道下次尝试写作的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 验证可用网络连接的最简单方法是什么? - What's the easiest way to verify there's an available network connection? 在C#中获取Flash文件运行时间的最可靠方法是什么? - What's the most reliable way to get a Flash file's run time in C#? 在 xUnit 中验证集合大小的惯用方法是什么? - What's the idiomatic way to verify collection size in xUnit? 在 Windows Forms 中将焦点设置为深度嵌套控件的最可靠方法是什么? - What's the most reliable way to set focus to a deeply nested control in Windows Forms? 从套接字接收消息时确定可变消息长度的可靠方法 - Reliable way to determine variable message length when receiving from socket 监视套接字以获取新数据然后处理该数据的最佳方法是什么? - What's the best way to monitor a socket for new data and then process that data? 什么是验证方法访问权限的最优雅方法(C#.Net) - What's the most elegant way to verify access rights on a method (C#.Net) 检查浮点变量是否为整数的最可靠方法是什么? - What is the most reliable way of checking if a floating point variable is an integer? C#.NET检查浏览器是否可移动 - 最可靠的方法是什么? - C# .NET Checking if browser is mobile - what is the most reliable way? 返回从存储过程中插入的记录数的可靠方法是什么 - what is a reliable way to return number of records inserted from a stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM