Java Internet Programming, Part 2
Thornton Rose
Published as "Java Internet Programming: Level 2", 11/16/99,
Gamelan.com.
Copyright © 2001, Thornton Rose.
This is part two of a three part series on writing Java programs that
use and implement Internet services. In part 1,
I covered protocols, sockets, and ports, and showed a simple client program.
In this part, I show you some more client programs, some server programs,
and the basics of using UDP.
Tools
The tools that you will need are:
- The Java Development Kit (JDK)
- Your favorite text editor.
Also, you will need access to a Simple Mail Transfer Protocol (SMTP) server.
Most likely, you can use the mail server at your Internet service provider
(ISP), your company, or your school.
Daytime, Too
Daytime
is the simple client
program that was shown in part one of this series. It is a program that uses
TCP and the daytime protocol to get the date and time from a daytime server.
UdpDaytime
is an implementation of
Daytime
that uses Universal Datagram Protocol (UDP) instead of TCP.
Here is the algorithm:
- Get the host from the command-line. If it's blank, print an error message and exit.
- Get the host's IP address.
- Create a datagram (UDP) socket.
- Create a datagram packet to send to the host and one to receive from the host.
- Set the socket I/O timeout to 5 seconds.
- Send the outgoing packet to the host.
- Receive the packet that comes back from the host.
- Print the data that was received.
- Close the socket and exit.
Notice the following differences between Daytime
and
UdpDaytime
:
- In the UDP program, you don't have to specify a host and port when you open
a datagram socket. Those are specified in the datagram packet that is sent out.
- With UDP, you don't use the I/O streams of the socket to send and receive
data. Instead, you send and receive packets; the data is in buffers that are
associated with those packets.
Echo, Echo
Echo is a simple internet protocol that is used for testing. Any data that is
sent to an echo server is sent back to the client. In this section, I present
both TCP and UDP implementations of an echo client.
TcpEcho
is a TCP implementation
if an echo client. Here is the algorithm:
- Get the host from the command-line. If it's blank, print an error message and
exit.
- Get the host's IP address.
- Open a socket to connect to the host on port 7.
- Set the socket I/O timeout to 5 seconds.
- Get a reference to the socket's I/O streams.
- Begin loop to send 3 requests:
- Start the timer.
- Write a request, a buffer that contains the request number as a byte, to
the output stream.
- Read a byte from the input stream.
- Print the elapsed time.
- End loop.
- Close the socket and exit.
UdpEcho
is a UDP implementation of
an echo client. Here is the algorithm:
- Get the host from the command-line. If it's blank, print an error message and
exit.
- Get the host's IP address.
- Open a datagram socket.
- Set the socket I/O timeout to 5 seconds.
- Create a request packet with a buffer than contains 32 spaces, and create a
packet for the reply.
- Begin loop for the number of requests to send:
- Start the timer.
- Send the request packet.
- Receive the reply packet.
- Print the number of bytes received and the elapsed time.
- End loop.
- Close the socket and exit.
Got Mail?
Now, let's look at an interesting little program called
MailPing
. I wrote it for testing purposes while
working at a company that had a very flakey mail server. It will check
connectivity to a mail server and send out a test message if specified. Here
is the algorithm:
- Get the command-line parameters: host, recipient, and sender. If no parameters
are specified, print an error message and exit. If sender is not specified, make it
the same as the recipient.
- Open a socket to connect to the host on port 25.
- Get the input and output streams for the socket.
- Read the initial reply from the host.
- If the recipient was specified:
- Create a simple test message.
- Send a greeting to the server and read the reply.
- Send the sender's address and read the reply.
- Send the data command and read the reply.
- Send the message and read the reply.
- Send the quit command and read the reply.
- Close the socket and exit.
Serving Up Daytime
Here I present TCP and UPD implementations of a server for the
daytime protocol. It will send the current date and time to any client
that connects to it.
DaytimeServer
is a TCP
implementation of a daytime server. Here is the algorithm:
- Open a server socket on port 13.
- Begin loop.
- Wait for a client connection, getting a reference to the client socket
(the server end of the connection between client and server).
- Get a reference to the output stream of the client socket.
- Write the date & time to the output stream.
- Close the client socket.
- End loop.
- Close the server socket and exit.
UdpDaytimeServer
is a UDP
implementation of a daytime server. Here is the algorithm:
- Open a datagram socket on port 13.
- Begin loop.
- Create a reply packet and wait for data to be received.
- When a packet is received, get the IP and port of the sender (client).
- Create a reply packet containing the date & time that can be sent back
to the client at its IP and port.
- Send the reply packet.
- End loop.
- Close the socket and exit.
Serving Up Echo
TcpEchoServer
is a TCP implementation
of a server for the echo protocol. It is similar to DaytimeServer, but I have
added a twist. Each client connection is serviced in a separate thread, so
that the main thread is free to do nothing but accept connections as
quickly as it can. Here is the main algorithm:
- Get the port from command-line. If it is not specified, use port 7.
- Create an instance of the server and start it.
- Create the server socket.
- Begin loop.
- Wait for client connection, getting a reference to the client socket when
the connection is received.
- Create and start a new client thread, passing it the client socket.
- End loop.
- Close the server socket and exit.
Here is the algorithm for the client thread (implemented as the inner class
ClientThread
in TcpEchoServer
):
- Get references to the input and output streams for the client socket.
- Begin loop.
- Read a byte from the input stream (blocking until a byte is received, or
end-of-file is encountered, which will happen when the client disconnects).
- Write the byte that was received to the output stream (i.e. send the byte back to
the client).
- End loop.
Writing a UDP implementation of an echo server is left as an exercise for
the reader.
Summary
In this article, I have shown you several client programs, a couple of server programs,
and the basics of using UDP. Hopefully these things have given you some ideas for your
own Internet programs. In the next part of this series, I will show you how to work with
some of the higher level protocols, such as HTTP.
Related Links