On this page are two links to Java programs that demonstrate the fundamentals of writing a server and a client program. The server accepts a line of text from the client, reverses it, and sends it back.  The client then displays the reversed line.

Both are Java applications.  To run on your local machine, do the following:

Compile the server program.  It produces 2 class files.  Start the server by opening a DOS window and type the following:

path-to-java-tools\bin\java Server  (You will see it prints a message saying it is listening on port 6789.)

 

Compile the client program.  It produces one class file.  Start the client by opening a DOS window and type the following:

path-to-java-tools\bin\java Client 127.0.0.1 6789

127.0.0.1  is an ip address for your local machine (that's where the server is running)

6789 is the port number

The client program reads these two command line arguments and uses them.


The server program can be run on any server machine and any port. To change the port assignment, pass the new port number as an argument when invoking the server.  If you run the server on a different machine than the client, simply pass the correct name of the server machine (when invoking the client) as the first argument.  Port number is not really required. If it is left off, the program defaults to port 6789.

Server.java

Client.java