Nada

Server on your own computer (lab 3 - Rock-Paper-Scissors)

The program of lab 3 connects to a server. Here is the server program. If the server is not running, you can run it locally, i.e. on your own computer.

Server

If you want to run your own server, just save the code above to your own account. Compile and run the server like this:

javac Server4712.java
java Server4712
The server will now run forever and just wait for clients to connect and play Rock-Paper-Scissors. When you are done with your server, you must terminate it yourself. This can be done by pressing Ctrl-C (Control button and c at the same time) in the terminal window where the server was started. If you have problems with this, just ask a lab assistant!

In some of the lab rooms, all students use the same computer. This may cause problems for the Rock-paper-scissors-server. Only one program at a time may use a particular port. If another student is already running the program using e.g. port 4712, your server program cannot use that port. It is possible for you to use the other students' server but a problem with this is that you don't know when this server shuts down. A more reliable solution for you is to use a port number of your own for your server. If you wish to do this, change "4712" to another number in the following line of code:

ServerSocket sock = new ServerSocket(4712,100);
Numbers less than 1000 are not free to use on a Unix computer but otherwise there is a free choice, e.g. 1717:
ServerSocket sock = new ServerSocket(1717,100);

Klient

When you want to play with a server on your own computer, some changes in the client are also required. The code to connect to the server looks essentially like this:

  try                                                          {
     Socket socket=new Socket("my.nada.kth.se",4712)           ;
     BufferedReader in=new BufferedReader(new InputStreamReader(
                           socket.getInputStream()))           ;
     PrintWriter ut=new PrintWriter(socket.getOutputStream())  ;
     ut.println("Charlotta"); ut.flush()                       ;
     System.out.println(in.readLine())                         ;}
Here, the client tries to connect to a server of the computer "my" and use port number 4712. If you replace "my.nada.kth.se" with "localhost", the client will find the server on the same computer it is running itself. Don't forget to change the port number in the server:
  try                                                          {
     Socket socket=new Socket("localhost",1717)                ;
     BufferedReader in=new BufferedReader(new InputStreamReader(
                           socket.getInputStream()))           ;
     PrintWriter ut=new PrintWriter(socket.getOutputStream())  ;
     ut.println("Charlotta"); ut.flush()                       ;
     System.out.println(in.readLine())                         ;}


Published by: <jsh@nada.kth.se>
Updated: 2008-04-16
Technical support: <webmaster@nada.kth.se>