Internet addresses are looked up in a series of hierarchically cached servers. That means
that your local computer might know a particular name-to-IP-address mapping automatically,
such as for itself and nearby servers. For other names, it may ask a local DNS server for IP
address information. If that server doesn't have a particular address, it can go to a remote
site and ask for it. This can continue all the way up to the root server. This process might
take a long time, so it is wise to structure your code so that you cache IP address information
locally rather than look it up repeatedly.
Inet4Address and Inet6Address
Beginning with version 1.4, Java has included support for IPv6 addresses. Because of this, two
subclasses of InetAddress were created: Inet4Address and Inet6Address. Inet4Address
represents a traditional-style IPv4 address. Inet6Address encapsulates a new-style IPv6
address. Because they are subclasses of InetAddress, an InetAddress reference can refer
to either. This is one way that Java was able to add IPv6 functionality without breaking
existing code or adding many more classes. For the most part, you can simply use
InetAddress when working with IP addresses because it can accommodate both styles.
TCP/IP Client Sockets
TCP/IP sockets are used to implement reliable, bidirectional, persistent, point-to-point,
stream-based connections between hosts on the Internet. A socket can be used to connect
Java's I/O system to other programs that may reside either on the local machine or on any
other machine on the Internet.
NOTE Applets may only establish socket connections back to the host from which the applet was
downloaded. This restriction exists because it would be dangerous for applets loaded through
a firewall to have access to any arbitrary machine.
There are two kinds of TCP sockets in Java. One is for servers, and the other is for
clients. The ServerSocket class is designed to be a "listener," which waits for clients to
connect before doing anything. Thus, ServerSocket is for servers. The Socket class is for
clients. It is designed to connect to server sockets and initiate protocol exchanges. Because
client sockets are the most commonly used by Java applications, they are examined here.
The creation of a Socket object implicitly establishes a connection between the client
and server. There are no methods or constructors that explicitly expose the details of
establishing that connection. Here are two constructors used to create client sockets:
Socket(String hostName, int por t)
Creates a socket connected to the named host
and por t.
Socket(InetAddress ipAddress, int por t)
Creates a socket using a preexisting
InetAddress object and a por t.
Search WWH :