COMP361: Client-Server socket programming using  TCP/UDP

Last revision 02/19/2004

This code is provided for your benefit.  Nothing needs to be submitted.

Running the programs on this page will help you understand how to do both TCP and UDP socket programming in Java and the difference between the two. You should be aware that these programs are slightly generalized versions of the code given in the book.  The difference is that the code here  permits providing the hostname of the server and the port number being used in the command line when calling the program rather than having them hard-coded into the actual code as the book has it.

For further explanation as to how this code works please see this page.

Note:  These programs have been tested on the lab2 machines csl2su1.cs.ust.hk  --  csl2su41.cs.ust.hk. In this lab you will be asked to log on to different machines at the same time (one to run the server,  one to run the client). In order to do this you should work on one lab 2 machine and ssh on to another lab 2 machine.

Also,  please note that this code has been observed to fail on some machines outside of lab 2.  In particular, some cs department machines outside of lab 2  are unable to resolve hostnames so if you typed in something like rabbit.cs.ust.hk as a hostname your program would not work.  If you are attempting to run this code on non lab 2 machines and encounter this problem  substitute the actual IP address for the hostname.

TCP:

  1. Download the two programs TCPClient.java and TCPServer.java onto lab2 machines
  2. compile the code  by running   javac TCPServer.java   and   javac TCPClient.java
  3. Have windows open on three lab 2 machines at same time
  4. Now try starting a client program first; by running java TCPClient  <server hostname> <port> before the server is started.  What happens?  Why?

 

UDP:

  1. Download the two programs UDPClient.java and UDPServer.java into lab2 machines
  2. compile the code  by running   javac UDPServer.java   and   javac UDPClient.java
  3. Have windows open on three lab 2 machines at same time
  4. Now try starting a client program first;
  5. Now modify the above by doing the following