in reply to To know the existence of an ip address

The point of gethostbyname is to resolve hostnames into IP addresses. If you pass an IP address, there is nothing to resolve, so, of course, you get back the IP address.

It sounds like what you want to do is see if there is a host "out there" that will respond to an IP address. You could try gethostbyaddr. That function maps an IP address to a host. If there is no associated host, it will fail.

However, success may not mean much. Most importantly, it won't tell you if the remote IP address is connected to the inter/intranet and ready to recieve requests. Many systems cache IP-host address associations. Unless you clear the cache, you could get a hostname back without even being on line. Even if you are on line, getting a host name back still doesn't tell you if the IP address is on-line. All it means is that you successfully connected to a domain name server (DNS) that has an entry for the hostname-IP address association.

To see if the IP address is on-line, you'd need to actually connect to the other machine. A common way to do that is "ping" (see Net::Ping or ping). However, pinging isn't 100% reliable. Some internet service providers (see Wikipedia) are afraid of ping being used for a DOS attack and do not allow pings to enter into their networks from the outside. Systems and local networks may be configured to block outgoing ping requests as well. (I seem to recall that Vista's default firewall configuration blocks either incoming or outgoing ICMP (the protocol used by ping) requests, but it has been a while. In any case if you use ping on a machine/network with a firewall, do check your firewall configuration if you have problems.

Alternatively, if you are having trouble using ping and you know something about the services offered by the remote IP address, you can try another protocol. For example, you could try sending an http, ssh, or ftp request and see if you get a response.

Best, beth

  • Comment on Re: To know the existence of an ip address