Bruce Eckel’s Thinking in Java | Contents | Prev | Next |
course, in order to tell one machine from another and to make sure that you are
connected with the machine you want, there must be some way of uniquely identifying
machines on a network. Early networks were satisfied to provide unique names
for machines within the local network. However, Java works within the Internet,
which requires a way to uniquely identify a machine from all the others
in
the world
.
This is accomplished with the IP
(Internet Protocol) address that can exist in two forms:
- The
familiar DNS
(Domain Name Service) form. My domain name is
bruceeckel.com,
so suppose I have a computer called
Opus
in my domain. Its domain name would be
Opus.bruceeckel.com.
This is exactly the kind of name that you use when you send email to people,
and is often incorporated into a World-Wide-Web address. - Alternatively,
you can use the “dotted
quad” form, which is four numbers separated by dots, such as
123.255.28.120.
both cases, the IP address is represented internally as a 32-bit number
[63]
(so each of the quad numbers cannot exceed 255), and you can get a special Java
object to represent this number from either of the forms above by using the
static
InetAddress.getByName( )
method that’s in
java.net.
The result is an object of type
InetAddress
that you can use to build a “socket” as you will see later.
a simple example of using
InetAddress.getByName( ),
consider what happens if you have a dial-up Internet service provider (ISP).
Each time you dial up, you are assigned a temporary IP address. But while
you’re connected, your IP address has the same validity as any other IP
address on the Internet. If someone connects to your machine using your IP
address then they can connect to a Web server or FTP server that you have
running on your machine. Of course, they need to know your IP address, and
since it’s assigned each time you dial up, how can you find out what it is?
following program uses
InetAddress.getByName( )
to produce your IP address. To use it, you must know the name of your computer.
It has been tested only on Windows 95, but there you can go to
“Settings,” “Control Panel,” “Network,” and
then select the “Identification” tab. “Computer name”
is the name to put on the command line.
//: WhoAmI.java // Finds out your network address when you're // connected to the Internet. package c15; import java.net.*; public class WhoAmI { public static void main(String[] args) throws Exception { if(args.length != 1) { System.err.println( "Usage: WhoAmI MachineName"); System.exit(1); } InetAddress a = InetAddress.getByName(args[0]); System.out.println(a); } } ///:~
my case, the machine is called “Colossus” (from the movie of the
same name, because I keep putting bigger disks on it). So, once I’ve
connected to my ISP I run the program:
WhoAmI Colossus
get back a message like this (of course, the address is different each time):
I tell my friend this address, he can log onto my personal Web server by going
to the URL
http://199.190.87.75
(only as long as I continue to stay connected during that session). This can
sometimes be a handy way to distribute information to someone else or to test
out a Web site configuration before posting it to a “real” server.
Servers
and clients
whole point of a network is to allow two machines to connect and talk to each
other. Once the two machines have found each other they can have a nice,
two-way conversation. But how do they find each other? It’s like getting
lost in an amusement park: one machine has to stay in one place and listen
while the other machine says, “Hey, where are you?”
machine that “stays in one place” is called the server,
and the one that seeks is called the client.
This distinction is important only while the client is trying to connect to the
server. Once they’ve connected, it becomes a two-way communication
process and it doesn’t matter anymore that one happened to take the role
of server and the other happened to take the role of the client.
the job of the server is to listen for a connection, and that’s performed
by the special server object that you create. The job of the client is to try
to make a connection to a server, and this is performed by the special client
object you create. Once the connection is made, you’ll see that at both
server and client ends, the connection is just magically turned into an IO
stream object, and from then on you can treat the connection as if you were
reading from and writing to a file. Thus, after the connection is made you will
just use the familiar IO commands from Chapter 10. This is one of the nice
features of Java networking.
Testing
programs without a network
many reasons, you might not have a client machine, a server machine, and a
network available to test your programs. You might be performing exercises in a
classroom situation, or you could be writing programs that aren’t yet
stable enough to put onto the network. The creators of the Internet Protocol
were aware of this issue, and they created a special address called localhost
to be the “local
loopback” IP address for testing without a network. The generic way to
produce this address in Java is:
addr = InetAddress.getByName(null);
you hand
getByName( )
a
null,
it defaults to using the
localhost.
The
InetAddress
is what you use to refer to the particular machine, and you must produce this
before you can go any further. You can’t manipulate the contents of an
InetAddress
(but
you can print them out, as you’ll see in the next example). The only way
you can create an
InetAddress
is through one of that class’s
static
member methods
getByName( )
(which is what you’ll usually use),
getAllByName( ),
or
getLocalHost( ).
can also produce the local loopback address by handing it the string
localhost:
by using its dotted quad form to name the reserved IP number for the loopback:
Port:
a unique place
within
the machine
IP address isn’t enough to identify a unique server, since many servers
can exist on one machine. Each IP machine also contains
ports,
and when you’re setting up a client or a server you must choose a port
where both client and server agree to connect; if you’re meeting someone,
the IP address is the neighborhood and the port is the bar.
port is not a physical location in a machine, but a software abstraction
(mainly for bookkeeping purposes). The client program knows how to connect to
the machine via its IP address, but how does it connect to a desired service
(potentially one of many on that machine)? That’s where the port numbers
come in as second level of addressing. The idea is that if you ask for a
particular port, you’re requesting the service that’s associated
with the port number. The time of day is a simple example of a service.
Typically, each service is associated with a unique port number on a given
server machine. It’s up to the client to know ahead of time which port
number the desired service is running on.
system services reserve the use of ports 1 through 1024, so you shouldn’t
use those or any other port that you know to be in use. The first choice for
examples in this book will be port 8080 (in memory of the venerable old 8-bit
Intel 8080 chip in my first computer, a CP/M machine).
This means a maximum of just over four billion numbers, which is rapidly
running out. The new standard for IP addresses will use a 128-bit number, which
should produce enough unique IP addresses for the foreseeable future.