Recipe 16.2 Finding and Reporting Network Addresses


You want to look up a host's address name or number or get the address at the other end of a network connection.


Get an InetAddress object.


The InetAddress object represents the Internet address of a given computer or host. It has no public constructors; you obtain an InetAddress by calling the static getByName( ) method, passing in either a hostname like or a network address as a string, like All the "lookup" methods in this class can throw the checked UnknownHostException (a subclass of java.ioIOException), which must be caught or declared on the calling method's header. None of these methods actually contacts the remote host, so they do not throw the other exceptions related to network connections.

The method getHostAddress( ) gives you the numeric IP address (as a string) corresponding to the InetAddress. The inverse is getHostName( ) , which reports the name of the InetAddress. This can be used to print the address of a host given its name, or vice versa:

// From  String ipNumber = ""; String hostName = "";  System.out.println(hostName + "'s address is " +      InetAddress.getByName(hostName).getHostAddress( ));  System.out.println(ipNumber + "'s name is " +      InetAddress.getByName(ipNumber).getHostName( ));

You can also get an InetAddress from a Socket by calling its getInetAddress( ) method. You can construct a Socket using an InetAddress instead of a hostname string. So, to connect to port number myPortNumber on the same host as an existing socket, you'd use:

InetAddress remote = theSocket.getInetAddress( ); Socket anotherSocket = new Socket(remote, myPortNumber);

Finally, to look up all the addresses associated with a host a server may be on more than one network use the static method getAllByName(host) , which returns an array of InetAddress objects, one for each IP address associated with the given name.

A static method getLocalHost( ) returns an InetAddress equivalent to "localhost" or This can be used to connect to a server program running on the same machine as the client.

If you are using IPV6, you can use Inet6Address instead.

See Also

See NetworkInterface in Recipe 17.10 which lets you find out more about the networking of the machine you are running on.

There is not yet a way to look up services i.e., to find out that the HTTP service is on port 80. Full implementations of TCP/IP have always included an additional set of resolvers; in C, the call getservbyname("http", "tcp"); would look up the given service[1] and return a servent (service entry) structure whose s_port member would contain the value 80. The numbers of established services do not change, but when services are new or installed in nonroutine ways, it is convenient to be able to change the service number for all programs on a machine or network (regardless of programming language) just by changing the services definitions. Java should provide this capability in a future release.

[1] The location where it is looked up varies. It might be in a file named /etc/services on Unix or the services file in a subdirectory of \windows or \winnt under Windows; in a centralized registry such as Sun's Network Information Services (NIS, formerly YP); or in some other platform- or network-dependent location.

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: