Java Internet Programming, Part 1

Thornton Rose

Published as "Java and Internet Programming: Introduction", 11/9/99, Gamelan.com.

Copyright © 2001, Thornton Rose.


This article is part one of a three-part series on writing Java programs that use and implement Internet services. In this part, I cover the tools and resources you will need, the protocols that your programs will use, the basics of sockets, and a very simple client program.

Tools

There are only a couple of tools that you will need to write Java programs for the Internet. They are:

Access to a Unix server (particularly one that is not secured) will make doing some things easier and more interesting, but it is not necessary. Also, having two PCs on a LAN can be helpful, but that is not necessary either. All the example programs for this series of articles can be run on a standalone PC.

Protocols

Programs that use or implement Internet services communicate with each other according to rules defined by one or more of the Net protocols. There are many of these protocols, but those that form the foundation for all the others are Internet Protocol (IP), Transport Control Protocol (TCP), and User Datagram Protocol (UDP).

IP is the lowest-level protocol. It defines the format and rules for the transmission of data in packets called datagrams. I am not going to go into any detail on this protocol, because one cannot write programs in Java that use it directly. Its primary importance is that it is the base protocol.

TCP is built on top of IP (which is where we get the common abbreviation TCP/IP). It defines the format and rules for the transport of packets from program to program on the Internet and provides mechanisms for acknowledging the delivery of IP packets, for requesting re-transmission of lost packets, and for assembling received packets in the order in which they were sent.

UDP is an unreliable counterpart to TCP. It does not guarantee packet delivery, nor does it provide packet sequencing and re-transmission. This may not seem very useful, but UDP has less overhead than TCP and is often faster.

Ports

When opening a socket, both an Internet address and a port must be specified. The port is an integer in the range 1 to 65535. If you think of the Internet address as a street address, then the port number is like an apartment number.

Here is a list of common Internet protocols and the port numbers associated with them [1]:

SERVICE/PROTOCOL PORT DESCRIPTION
echo 7 Used to verify that two machines can connect by having one echo back the other's data.*
daytime 13 Provides an ASCII representation of the current time on the server.*
ftp 20/21 Used for file transfers. Port 21 is used for commands; port 20 is used for data.
telnet 23 Used for interactive, remote, command-line sessions.
smtp 25 Simple Mail Transfer Protocol, which is used to send email between machines.
whois 43 Simple directory service for network administrators.
finger 79 Used to get information about one or more users on the host machine.
http 80 HyperText Transfer Protocol, the underlying protocol of the World Wide Web.
pop3 110 Post Office Protocol Version 3, which is used to transfer e-mail from a host (e-mail server) to an e-mail client.
nntp 119 Network News Transfer Protocol, used to disseminate Usenet news.

* These protocols have both TCP and UDP implementations.

Sockets

Sockets are endpoints for communication between two machines [2], and subsequently between programs that use Internet protocols. Basically, they work like any other type of I/O object. You open them, read and/or write data, then close them when you are done.

In the Java API, sockets are provided via the classes in the java.net package. There are many classes in this package, to provide various levels of abstraction for the programmer, but the key classes are Socket, ServerSocket, DatagramSocket, DatagramPacket, and InetAddress. Here's a brief synopsis of each:

CLASS DESCRIPTION
Socket Communication endpoint used by a client to connect to a server using TCP.
ServerSocket Communication endpoint on which a server accepts connections from clients.
DatagramSocket Socket for sending and receiving UDP packets.
DatagramPacket Represents a UDP packet.
InetAddress Represents an Internet address.

Simple Client

By this point you are probably wondering how all the information I have presented so far can get you to writing internet programs in Java. So, let's take a look at a very simple TCP client.

Daytime is a simple program that uses the daytime protocol to get the date and time from a given host. Here is the algorithm:

  1. Get the host from the command-line. If it's blank, print an error message and exit.
  2. Get the host's IP address.
  3. Open a socket to connect to the host.
  4. Set the socket I/O timeout to 5 seconds. (This is an optional step, but the default timeout may be longer than you wish the program to wait for a response from the host.)
  5. Get a reference to the socket's input stream.
  6. Read a line from the input stream. (Note that the readLine() method blocks until either input is received or a timeout occurs.)
  7. Print the data that was received.
  8. Close the socket and exit.

When you run Daytime, you must specify the host machine (by IP or name) from which to get the date and time. If you have access to an unsecured Unix server, give it a try. If you want to run the program local:

  1. Run DaytimeServer (included in the zip file listed in the Related Links section) in a shell/DOS window.
  2. In a second shell/DOS window, run Daytime and specify "localhost" or 127.0.0.1 as the host.

Ping

Some ambitious readers may want to try writing a ping program in Java. It seems like it should be a simple program, but unfortunately it's not. Ping uses the Internet Control Message Protocol (ICMP), which is treated like a high-level protocol but is a actually just a part of IP. To send ICMP messages, you have to be able to directly manipulate the IP header, which requires using what are called "raw sockets" (ICMP sockets). The Java API doesn't support raw sockets, so you would have to implement them in C/C++ as native code.

Summary

In this article, I have presented the basics of Internet protocols, ports, and sockets, and have shown you a very simple Java program that uses the daytime protocol to get the date and time from a host machine. Hopefully, this information will help you take a solid first step toward writing your own Internet programs in Java. In the next part of this series, I'll show you some more client programs, some simple server programs, and how to use UDP.

Related Links

References

  1. Java Network Programming. Harold, Elliotte Rusty. Copyright 1997 O'Reilly and Associates, Inc.

  2. Java Platform 1.2 API Specification. Copyright 1993-1998 Sun Microsystems, Inc.