如何检测同一局域网中的PC是否断开连接?温索克,Icmp? [英] How do i detect if a PC on the same lan disconnects? Winsock,Icmp?

查看:76
本文介绍了如何检测同一局域网中的PC是否断开连接?温索克,Icmp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在编写一个C ++控制台应用程序,该应用程序经常应检测同一局域网上的PC是否仍处于连接状态.我有这种情况:

家庭局域网(3个,1个路由器)

客户-------路由器------- PC

我的应用程序在客户端上运行,它应该检测PC是否无法访问(例如,关闭计算机或断开电缆连接).
我有一些代码可以做到:

Hello there,
i''m coding a C++ console application that, very often, should detect if a PC on the same lan is still connected. I have this kind of situation:

HOME LAN (3pc, 1 router)

CLIENT ------- ROUTER ------- PC

My application, runs on the client and it should detect if the PC becomes unreachable (for example is shut down, or the cable is disconnected).
I have some code that does:

while(true){
          If(!PCisConnected())
                //Do Something and close app;
          Sleep(1000);
}



在这一点上,我需要对PcISConnected()函数进行编码.检测断开连接的最佳和最经济的方法是什么?
更确切地说,该PC并不是真正的PC,而是可以响应PING(ICMP ECHO)且具有Web服务器+ Ftp服务器的LAN UNIT.因此,基本上,它们可以检测到ICMP或TCP 80/21断开连接的方式.
我的第一个尝试在此LAN环境中并不是很好,而在WAN环境中却非常出色.
我创建了一个无限循环,在其中创建了一个用于端口80的TCP套接字并调用connect()函数.
它的运行效果很好,但是:如果PC断开连接,则平均需要20到30秒才能引发错误(如果PC是世界上某个地方的Web服务器,则运行得很好).
我的第二次尝试有一个类似的问题:
而不是将connect()陷入无限循环,我将send()放入.
因此,我尝试将消息发送到PC.
问题是,即使我断开了PC与LAN的连接,发送迭代10-20次也将引发错误.因此,大约20次,它说即使PC断开连接也成功发送了该消息(就像我之前说的那样,在世界各地使用Web服务器进行的相同测试非常有效,并且发送仅重复了2次,然后抛出错误). br/> 第三次尝试是ICMP,但我没有为我的项目找到任何合适的解决方案(通过Eclipse进行编码).但是我发现ICMP负担得起,因为有时,即使主机启动了,ping也会失败,并且该故障会导致应用程序出现故障,导致主机仍然启动.

我该如何解决这个问题?我在这里可以采取的最佳方法是什么?
它应该可以负担得起,而且速度很快,因为我应该在2-3秒内检测到断开连接,而不再是其他原因. 我知道,我需要不断地向PC发送消息,但这不是问题.
谢谢大家.



At this point i need to code the PcISConnected() function. What''s the best and the most affordable way to detect if it disconnects?
To be more precise, the PC is not a real PC but rather a LAN UNIT that can reply to PING (ICMP ECHO) and that has a Web Server + Ftp Server. So basically they way i can detect a disconnection is trough ICMP or TCP 80/21.
My 1st attempt weren''t so good in this LAN environment while they''re very great in an WAN environment.
I created an infinite loop in which i create a TCP Socket to the port 80 and call the connect() function.
It worked great BUT: if the PC is disconnected, it tooks an average 20-30seconds before throwing an error (while it worked very well if the PC was a webserver in the world, somewhere).
My 2nd attempt has a similiar issue:
Instead of putting the connect() in an infinite loop, i put the send().
So i''ve tryied to send a message to the PC.
The issue is that, even if i disconnect the PC from the LAN, the send iterate 10-20 times before throwing an error. So for around 20times, it says that the message was succesfully sent even if the PC is disconnected (as i said before, the same test with a webserver around the worl, worked great and the send iterated only 2times before throwing an error).
And the 3rd attempt is ICMP but i didn''t found any suitable solution for my project (which is being coded trough Eclipse). But i found ICMP not so affordable since sometimes, a ping fails even if the host is up and that failure will lead application to malfunction cause the host is still up.

How can i solve this issue? What''s the best approach i can take here?
It should be affordable and fast as i should detect a disconnection in 2-3 seconds, no more.
I know for that, i need to continuosly send message to the PC, but that''s not an issue.
Thanks you all.

推荐答案

我宁愿在单独的线程中定期ping PC.您将需要使用ICMP协议的实现,请参见 http://en.wikipedia.org/wiki/Ping [ ^ ].

—SA
I would rather ping the PC periodically, in a separate thread. You will need to use implementation of ICMP protocol, see http://en.wikipedia.org/wiki/Ping[^].

—SA


这篇关于如何检测同一局域网中的PC是否断开连接?温索克,Icmp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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