DatagramSocket NetworkInter face URLClassLoader
DatagramSocketImpl PasswordAuthentication URLConnection
HttpCookie (Added by
Java SE 6.)
Proxy URLDecoder
HttpURLConnection ProxySelector URLEncoder
IDN (Added by Java SE 6.) ResponseCache URLStreamHandler
Inet4Address SecureCacheResponse
Thejava.netpackage’s interfaces are listed here:
ContentHandlerFactor y DatagramSocketImplFactor y SocketOptions
CookiePolicy (Added by Java SE 6.) FileNameMap URLStreamHandlerFactor y
CookieStore (Added by Java SE 6.) SocketImplFactor y
In the sections that follow, we will examine the main networking classes and show
several examples that apply to them. Once you understand these core networking classes,
you will be able to easily explore the others on your own.
InetAddress
TheInetAddressclass is used to encapsulate both the numerical IP address and the domain
name for that address. You interact with this class by using the name of an IP host, which is
more convenient and understandable than its IP address. TheInetAddressclass hides the
number inside.InetAddresscan handle both IPv4 and IPv6 addresses.
Factory Methods
TheInetAddressclass has no visible constructors. To create anInetAddressobject, you
have to use one of the available factory methods.Factory methodsare merely a convention
whereby static methods in a class return an instance of that class. This is done in lieu of
overloading a constructor with various parameter lists when having unique method names
makes the results much clearer. Three commonly usedInetAddressfactory methods are
shown here:
static InetAddress getLocalHost( )
throws UnknownHostException
static InetAddress getByName(StringhostName)
throws UnknownHostException
static InetAddress[ ] getAllByName(StringhostName)
throws UnknownHostException
ThegetLocalHost( )method simply returns theInetAddressobject that represents the local
host. ThegetByName( )method returns anInetAddressfor a host name passed to it. If these
methods are unable to resolve the host name, they throw anUnknownHostException.
Chapter 20: Networking 601