CPSC 170 Lab 13: Networking

Echo

In the last lab we learned how to create programs with multiple paths of execution using threads. In this lab you will learn to create multiple programs, each with its own path of execution, that can communicate using networking. Computers typically have a single connection to a network. All network communication for a particular computer is sent to the proper computer using a unique address. Since multiple programs can be running on a single computer, the operating system must route network communication to the proper process. This is done using port numbers. Each program that is using a network connection also has a port number, a 16-bit number. Some port numbers are reserved for the operating system and can not be used by your program. For example port 80 is used for HTTP communication. The reserved ports are all less than 1023.

In Java, network communication is implemented using sockets. A socket is one endpoint of a two-way network communication between two programs. A socket is bound to a particular network address and to a port number so that all communication through the socket is routed to the correct machine on the network and to the right program on the machine. In Java there are two different classes that represent sockets, a server socket and a client socket. A server is a program that runs waiting for a connection request on a particular port. A client is a program that can request a connection to the server. When the server receives a connection request it generates a new client socket to use to communicate with the client. If the server accepts the connection request then the client creates a client socket to use to communicate with the server.

The file EchoServer.java contains server program that once it establishes a connection with a client reads strings from the socket, prints the strings to the console, and sends the string back to the client. Download this application and look at the code. Notice how it uses the class ServerSocket to create a connection to a client. It also creates a PrintWriter and a BufferedReader to simplify sending strings over the network. Also note at the end of the program the socket is closed just as the input and output streams are. Run the program and notice that it doesn't do very much. For it to do any more it needs a client to communicate with.

The file EchoClient.java contains a client program for the echo server. The program reads strings from the user, sends them to the sever, and prints the server's response. Download this application and look at the code. Notice that it uses the class Socket to create a connection to the server. Just like the server it creates a PrintWriter and a BufferedReader to facilitate sending and receiving strings and it closes the streams and the socket at the end of the program. It is possible to run the client and the server on the same computer. Begin by first running the server (if it is not still running). Copy the network address that the server is running on and set the host address String in the client program to be this address. Then run the client program. Both of the programs output to different consoles in the Eclipse window. In order to switch between them use the button that looks like a computer monitor in the console tab. It is also possible to run each of the programs on different computers.

Chat

Create a simple text chat program. Create a program that implements both the client and server. When the program is first run it should query the user if they would like to host a chat or join a chat. If they choose to host then the program should create a server socket and display the server's network address. If they choose to join a chat then the program should prompt the user to enter the network address of a server.

Once the connection is established the program should create instance variables for a PrintWriter and a BufferedReader (whether the program is running a server or a client). In order to both read the input from the socket and input from the user the program will then need to create a thread. Every time the program reads text from the socket it should display it to the command line. Every time the user types text it should write it to the socket.

Networked Pong

Create a two player version of pong that works over the network. You can use your own code from previous labs or use the code in the file Pong.tar. The program should implement both the server and the client. When the program is run it should display a dialog asking the user if they would like to host or join a game.

Begin by creating a program where the user of the server program can control a paddle that also appears in the client window. The location of the paddle in the server program is determined by the mouse listener. Every time the position is updated it should send this information to the client. The client should check its socket every frame of animation to see if the location of the paddle has been updated. If it has it should change the location of the paddle in its display. Note that the readLine method of the BufferedReader class is a blocking method, that is, the program stops execution until there is something to read. The client will therefore block if the server does not move its paddle. To solve this, use the ready method in the BufferedReader class to determine if there is any text in the buffered reader. If there is not any text in the buffered reader the readLine method will stop execution, so do not call it.

Next, add the paddle that is controlled by the client. The process is very similar to above. The client paddle should be updated when the mouse moves and it should send the updated position to the server. Notice that it is possible for the ball to become out of sync between the client and server. To fix this one of the programs (either the client or the server) should be responsible for animating the ball. It should send strings to the other program every time the velocity of the ball changes. These string should contain the coordinates of the ball as well as its velocity and be formatted such that the other program can distinguish them from updates of the paddle location.

Hand In: Tar your lab directory and e-mail it to your instructor with cpsc170 lab13 in the subject.