开发者

How to check if a given ip address is reachable in C program

开发者 https://www.devze.com 2023-04-10 06:25 出处:网络
I need a C program that will check whether开发者_如何学Python 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

I need a C program that will check whether开发者_如何学Python 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号