如何在C程序中检查给定的IP地址是否可达
我需要一个 C 程序来检查用户给定的 IP 地址(ipv4 和 ipv6)是否可达。我不知道端口号。所以我无法连接和检查。该功能或多或少类似于 ping。但我不想使用 ping,因为涉及到开销。
I need a C program that will check whether a user given ip address(both ipv4 and ipv6) is reachable or not. I do not know the port no. So I cant connect and check. The functionality is more or less like ping. Except I don't want to use ping because of the overhead involved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建一个原始套接字,然后构造一个 ICMP 数据包并将其发送到您的目的地。
http://courses.cs.vt.edu/~cs4254/fall04 /slides/raw_6.pdf
You need to create a raw socket, then construct and send an ICMP packet to your destination.
http://courses.cs.vt.edu/~cs4254/fall04/slides/raw_6.pdf
主机可访问并不意味着您能够从中获得服务。最好的方法是尝试连接到正确的端口号。您到底想要实现什么目标?
此外,虽然 ICMP 是执行此操作的明智方法,但如果主机有禁用 ICMP 的防火墙,则它可能无法工作。在这种情况下,您将不会得到任何回复。
Having an host reachable doesn't mean you will be able to get service from it. The best would be to try to connect to the correct port number. What are you trying to achieve exactly ?
Besides, although ICMP is the smart way to doing it, it might not work if the host has a firewall disabling ICMP. In that case you won't get any response.