Java Reference
In-Depth Information
Chapter 2
Starting Network Programming in Java
Learning Objectives
After reading this chapter, you should:
￿ know how to determine the host machine's IP address via a Java program;
￿ know how to use TCP sockets in both client programs and server programs;
￿ know how to use UDP sockets in both client programs and server programs;
￿ appreciate the convenience of Java's stream classes and the consistency of the
interface afforded by them;
appreciate the ease with which GUIs can be added to network programs;
know how to check whether ports on a specifi ed machine are running services.
Having covered fundamental network protocols and techniques in a generic fashion
in Chap. 1 , it is now time to consider how those protocols may be used and the
techniques implemented in Java. Core package contains a number of
very useful classes that allow programmers to carry out network programming very
easily. Package java x .net , introduced in J2SE 1.4, contains factory classes for creating
sockets in an implementation-independent fashion. Using classes from these packages
(primarily from the former), the network programmer can communicate with any
server on the Internet or implement his/her own Internet server.
The InetAddress Class
One of the classes within package is called InetAddress , which handles
Internet addresses both as host names and as IP addresses. Static method getByName
of this class uses DNS (Domain Name System) to return the Internet address of a
specifi ed host name as an InetAddress object. In order to display the IP address from
this object, we can simply use method println (which will cause the object's toString
method to be executed). Since method getByName throws the checked exception
Search WWH ::

Custom Search