Java Reference
In-Depth Information
Here are two example runs of this program:
c:
\
>
java TranslateAddress gluon.particle.kth.com
Address gluon.particle.kth.se =
gluon.particle.kth.se/
130.237.34.133
Name of gluon.particle.kth.se = gluon.particle.kth.se
c:
\
> java TranslateAddress 130.237.34.133
Address 130.237.34.133 = /130.237.34.133
Name of 130.237.34.133 = gluon.particle.kth.se
Note that the method
getByName (String str)
returns an instance of
InetAddress
when given a host name. Then when the
toString()
method of
this
InetAddress
object is invoked by the string concatenation, it displays the
HostName/IP
-
Address
pair. However, when given an IP address, the result-
ing
InetAddress
object does not show the host name. Instead, you can use the
getHostName()
method to obtain the host name.
13.7 Sockets
Sockets provide connections between applications that allow streams of data
to flow. Sockets in Java are straightforward to set up. The package
java.net
provides two kinds of socket classes:
1.
Socket
- provides a connection-oriented service that behaves like telnet or ftp. The
connection remains active, even with no communications occurring, until explicitly
broken.
2.
DatagramSocket
-involves the following:
(a) connectionless protocol
(b) transfer of datagram (i.e. UDP) packets
(c) no fixed connection
(d) packets can arrive out of order
(e) no guarantee a packet will arrive.
We discuss sockets further in Chapters 14 and 15. The demonstration program
below illustrates how to use a socket connection to run the
whois
internet operation
[2]. The
whois
service returns information about a given domain name. The
registry site
whois.internic.net
provides this service.
The code snippet below shows how to create an instance of the
Socket
class
that connects to port 43 at host name
whois.internic.net
.Anoutput stream
is obtained from the socket and a
PrintWriter
wrapped around it. This is then
used to send the address of interest to the
whois
service.
Similarly, an input stream is obtained from the socket and wrapped first
with an
InputStreamReader
and then a
BufferedReader
so we can use
readLine()
to obtain a whole line of text at one time from the
whois
output.
Yo u can enter other domain names in the text field.
Search WWH ::
Custom Search