JavaEnterprise JavaJava Internet Programming: Level 3

Java Internet Programming: Level 3

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


This is the final installment in a three-part series on writing Java programs that use and implement Internet services. In the previous two articles (see
Java Internet Programming: Level 2
) I covered protocols, ports, sockets, and UDP, and showed both client and server programs. There is so much that I would like to write about from here, but I don’t have the space. So, I am going to illustrate two things that hopefully will be interesting: a POP3 client and an HTTP server.

Tools

The tools that you will need for this article are:

  • The Java Development Kit (JDK)
  • Your favorite text editor.

Also, you will need access to a mail server that supports Post Office Protocol Version 3 (POP3). Most likely, you can use the mail server at your Internet service provider (ISP), your company, or your school.

POP3

Post Office Protocol Version 3 (POP3) is a high-level Net protocol that “is intended to permit a workstation to dynamically access a maildrop on a server host in a useful fashion” [Ref. 1]. In other words, it is a protocol designed to allow a client program to retrieve mail that is being held at a server.

The POP3 specification (RFC1939) is straightforward; but, like most RFCs, it is not what I would call lunchtime reading. The basic operation of POP3 is:

  1. The server listens for connections on port 110.
  2. A client connects and authenticates.
  3. The client sends commands; the server processes the commands and sends responses back to the client, until the client disconnects or aborts.

POP3 commands are single lines composed of a keyword, followed by one or more parameters, followed by a carriage return plus linefeed (CRLF). Responses from the server may be one or more lines. For either, the first line is composed of the status (+OK or -ERR), followed by additional information, followed by CRLF. For a multi-line response, the last line is composed of a period (“.”) followed by CRLF.

Here is a synopsis of the POP3 commands:

COMMAND DESCRIPTION

STAT Get status, referred to as a “drop listing” — number of messages waiting and size of maildrop in octets.
LIST [msg] Get “scan listing” — message number and size in octects — for all messages or specified message (e.g., 1, 2, 3).
RETR msg Retrieve specified message.
DELE msg Mark specified message for deletion.
NOOP Do nothing.
RSET Unmark any messages that were marked for deletion.
QUIT End the session.
TOP msg n Retrieve header and first n lines of specified message.
UIDL [msg] Get the unique identifier of all messages or the specified message.
USER name Identify mailbox (username) to access.
PASS pw Send password for mailbox specified by USER command. (Note: The password is sent in clear text.)
APOP name digest Send mailbox (username) and MD5 digest string (similar to an encryption key). This command can be used instead of USER + PASS to authenticate, so that the password does not get sent in clear text.

MailStat

Listing 1
is the code for

MailStat
, a program that illustrates the basics of using POP3. It is a simple program that checks for mail on a given server. Here is the algorithm:

  1. Get the command-line parameters – host, username, and password. If they are not specified, print a message and exit.
  2. Get the host’s IP address.
  3. Open a socket to the host.
  4. Get references to the socket input and output streams.
  5. Read the reply from the server.
  6. Send the username and read the reply.
  7. Send the password and read the reply.
  8. Get the status (number of messages, mailbox size).
  9. End the session.
  10. Close the socket and exit.

I could have done more with

MailStat
, but I wanted to keep it simple, so that “features” did not get in the way of illustrating POP3.

MailStat
was meant to be a stepping stone. Some suggestions for making it more useful are: the ability to check for mail every x minutes, the ability to check more than one mailbox, and a GUI.

HTTP

HyperText Transport Protocol (HTTP), another high-level Net protocol, “is the standard protocol for communication between web browsers and web servers” [Ref.2]. The specification for HTTP 1.1 can be found in RFC 2616, and the specification for HTTP 1.0 can be found in RFC 1945.

The basic operation of HTTP is:

  1. The server listens for connections on port 80.
  2. A client connects and sends a request.
  3. The server sends a response, which includes the contents of a file if it was requested.
  4. The connection is closed by the client, the server, or both.

Client Request

The general form of a client request.

Here are some examples:


1. GET /foo.html HTTP/1.1

2. GET /foo.html HTTP/1.1
Accept: text/html
Accept: text/plain
Accept: image/gif
Accept: image/jpg
User-Agent: Netscape/4.5

Server Response

The general form of a server response.

Here is an example:


HTTP/1.1 200 OK
Server: NCSA/1.4.2
MIME-version: 1.0
Content-type: text/html
Content-length: 64

<html>
<head><title>Foo</title></head>
<body>Foo</body>
</html>

HttpServer

Listing 2
is the code for

HttpServer
, a very simple HTTP (web) server. Note that, like

MailStat
,

HttpServer
is lean. It is meant to be a starting point that illustrates HTTP. I implemented it with only two classes, and it supports only the GET command for HTML, text, GIF, and JPEG files. Here is the algorithm:

  1. Get the command-line parameters: port. If the port is not specified, set it to 80.
  2. Open a server socket on the specified port.
  3. Begin loop.
    1. Wait for client connection, getting a reference to the client socket when the connection is received.
    2. Create and start a new request thread, passing it the client socket.
  4. End loop.
  5. Close the server socket and exit.

The code for the request thread is shown in
Listing 3
. Here is the algorithm:

  1. Get references to the socket input and output streams.
  2. Print the request information.
  3. If the command was “GET”, send the response. If the file is found, it it is included as the content of the response. If it is not found, send “404 Not Found”.

Summary

This is the end of the line for this series of articles. Hopefully, my example programs were interesting and provided you with good starting points for your own Internet programming in Java.

Related Links

References

  1. RFC 1939, “Post Office Protocol — Version 3 (POP3)”. Myers, J., Carnegie Mellon; Rose, M., Dover Beach Consulting, Inc., May 1996.
  2. Java Network Programming. Harold, Elliotte Rusty. Copyright 1997, O’Reilly & Associates, Inc.

About the author

Thornton Rose is a contract software developer in Atlanta, Ga. He can be reached via e-mail at trose@avana.net.



Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories