In this discussion, we begin considering Java networking facilities. The emphasis here is on facilities needed to implement simple client/server architectures. A subsequent tutorial will continue the discussion.The relevant classes are contained in the java.net package. You should read the classes and methods closely, paying particular attention to the
ServerSocket
,Socket
, andInetAddress
classes.The discussion assumes that you are familiar with the concepts of sockets, connections, and basic client/server architecture. If not, you may wish to look at the course tutorial on WWW Client/Server Architecture and/or a textbook on the subject.
Because of concerns for security, the developers of the popular browsers, such as Netscape, have built into their systems restrictions on the IP addresses to which an applet can connect. Consequently, Java client/server programs are often written as applications, rather than as applets. As a result, most of this discussion will be cast in the context of applications, although the last example is an applet client. If you wish to run the applications discussed, you may do so by accessing the
programs
directory stored with this tutorial, do a view source through your browser, and store the Java code within you local file system; after that, you can execute them using the Java Virtual Machine (i.e.java file_name
.In developing your own client/server programs, you should keep in mind that it is very easy to produce a program that hangs. Consequently, you should always be prepared to
kill
the process manually. If you are working in Unix, a good way to do this is to use the following sequence of steps:The discussion, below, progresses through three steps. First, we look at a minimal server and client, then a slightly more advanced pair, and finally at a client applet.
- java file_name & /* to run process in background so you can kill
- ps /* to list your current processes
- kill -9 # /* where # is the number for the hung process
After discussing Java threads in the next tutorial, we will return to the client/server architecture and consider a multi-threaded design in the Java Clients and Servers II tutorial.
Minimal Client/Server
The goal of this discussion is to pair down the requirements for a client/server pair of applications to the bare essentials to illustrate establishing a connection between two processes running on different host machines. We'll discuss the server, first, and then a client that connects to an instance of this server that is already running. In this example, the client connects to the server and the server returns a "canned" message and breaks the connection. Thus, the client and server programs function at the "Hello, World!" level.Server
Client
Mid-Size Client/Server
The second set of client/server programs includes a good deal more flexibility than the first. It allows the user to see the host and port values and to alter them prior to connection. After connection, the user can type in a message, which the client sends to the server. The server returns the message to the client, which displays it.This set of client/server programs is considerably larger than the first, but the more elaborate user interface accounts for much of this difference in size.
Server
Client
Applet Client
The examples discussed above were built as Java applications. Here, we show a revised version of the mic-size client program just, recast as an applet. There are surprisingly few differences in the code, the main distinctions being subclassingApplet
instead ofFrame
and the use of aninit
method instead ofmain
.The most important distinction lies with how the server and client must be configured with respect to hosts. Security restrictions limit the IP address to which the client can connect to the IP address of the WWW server from which the applet was originally obtaianed. Thus, your server must be running on the same host machine as the WWW server that provides access to your client applet.
Client
References
References useful for this discussion include the following:
- Sun's Java Development Kit (JDK)
- Sun's Java API
- Java API Class Hierarchy
- Source Code for JDK Packages