Skip to Content
Java Network Programming, Second Edition
book

Java Network Programming, Second Edition

by Elliotte Rusty Harold
August 2000
Intermediate to advanced
760 pages
21h
English
O'Reilly Media, Inc.
Content preview from Java Network Programming, Second Edition

The InetAddress Class

The java.net.InetAddress class is Java’s encapsulation of an IP address. It is used by most of the other networking classes, including Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more.

public final class InetAddress extends Object implements Serializable

This class represents an Internet address as two fields: hostName (a String) and address (an int). hostName contains the name of the host; for example, www.oreilly.com. address contains the 32-bit IP address. These fields are not public, so you can’t access them directly. It will probably be necessary to change this representation to a byte array when 16-byte IPv6 addresses come into use. However, if you always use the InetAddress class to represent addresses, the changeover should not affect you; the class shields you from the details of how addresses are implemented.

Creating New InetAddress Objects

There are no public constructors in the InetAddress class. However, InetAddress has three static methods that return suitably initialized InetAddress objects, given a little information. They are:

public static InetAddress InetAddress.getByName(String hostName) 
 throws UnknownHostException
public static InetAddress[] InetAddress.getAllByName(String hostName) 
 throws UnknownHostException
public static InetAddress InetAddress.getLocalHost(  ) 
 throws UnknownHostException

All three of these may make a connection to the local DNS server to fill out the information in the InetAddress object. This has a ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Network Programming, 4th Edition

Java Network Programming, 4th Edition

Elliotte Rusty Harold
Java Concurrency, 2/e

Java Concurrency, 2/e

Douglas Schmidt

Publisher Resources

ISBN: 1565928709Supplemental ContentCatalog PageErrata