如何检测tcpclient不再连接到指定的端点。 [英] How to detect tcpclient is no more connected to designated endpoint.

查看:115
本文介绍了如何检测tcpclient不再连接到指定的端点。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我使用TCPClient仅用于从EndPoint读取数据。如何检测远程终点是否断开,以便重新连接可以执行。



Naina



我尝试了什么:



我们尝试使用套接字进行轮询,但是如何检测TCPClient的断开连接。

解决方案

只需从代码中ping服务器,并检查结果,如果得到响应,您的连接没有中断,否则您已断开连接。查看此链接以了解更多信息。


别。假设它已连接。只需根据应用层协议执行网络I / O,即发送和接收数据,如果失败,则处理异常。有关详细信息,请参阅我过去的答案:

http://www.codeproject.com/Answers/848979/How-Do-I-Get-To-Know-If-AA-Tcp-Connection- Is-Over#answer1 [ ^ ],

套接字编程中的业余问题 [ ^ ],

来自同一端口号码的多个客户端 [ ^ ]。



顺便说一句,请不要告诉我你不要没有这样的应用程序级协议。它只是意味着你隐含地拥有它,以某种方式假设应该做什么。但是,实现它并开发协议的全面定义总是更好。请参阅:应用层 - 维基百科,免费的百科全书 [ ^ ]。



-SA

Hello,

I am using TCPClient for only for reading data from EndPoint. How to detect the remote end point is disconnected, so that reconnect can be executed.

Naina

What I have tried:

This we have tried poll, available using socket, but how to detect disconnection for TCPClient.

解决方案

Hi, Just Ping the server from code, and check the results, if you got response your connection is not broken, else you have disconnected. Check this links to know more.


Don't. Assume it is connected. Just perform your network I/O according to your application-layer protocol, that is, send and receive data, and, if it fails, handle the exception. For more detail on that, please see my past answers:
http://www.codeproject.com/Answers/848979/How-Do-I-Get-To-Know-If-A-A-Tcp-Connection-Is-Over#answer1[^],
an amateur question in socket programming[^],
Multple clients from same port Number[^].

By the way, please don't tell me that you don't have such application-level protocol. It would simply mean that you have it implicitly, somehow assuming what is should do. But it's always better to realize it and develop a comprehensive definition of your protocol. Please see: Application layer — Wikipedia, the free encyclopedia[^].

—SA


这篇关于如何检测tcpclient不再连接到指定的端点。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆