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:
- Download the two programs
TCPClient.java and TCPServer.java
onto lab2 machines
- compile the code by running
javac TCPServer.java and
javac TCPClient.java
- Have windows open on three lab 2 machines at same time
- On machine one (the server) run
java TCPServer <port>
- <port> is any port
number you choose, e.g., 20000.
- Once <port> is
chosen above, it stays the same for the remaining commands
- On machine two (a client)
- run
java TCPClient <server hostname> <port>
- Type any sentence + CR
- See what is returned
- On machine three (a client)
- run
java TCPClient <server hostname> <port>
- Type any sentence + CR
- See what is returned
- Terminate the server program
- Now try starting a client program first; by running
java TCPClient <server hostname>
<port> before the server is started. What happens?
Why?
UDP:
- Download the two programs
UDPClient.java and UDPServer.java into
lab2 machines
- compile the code by running
javac UDPServer.java and
javac UDPClient.java
- Have windows open on three lab 2 machines at same time
- On machine one (the server) run
java UDPServer <port>
- <port> is any port
number you choose, e.g., 20000.
- Once <port> is
chosen above, it stays the same for the remaining commands
- On machine two (a client)
- run
java UDPClient <server hostname> <port>
- Type any sentence + CR
- See what is returned
- On machine three (a client)
- run
java UDPClient <server hostname> <port>
- Type any sentence + CR
- See what is returned
- Terminate the server program
- Now try starting a client program first;
- run java UDPClient <server
hostname> <port> on client
before the server is started.
- Do not type a sentence into client yet
- run java
UDPServer <port> on server
- type a sentence + CR into client
- Explain why result is different from TCP example
- Now modify the above by doing the following
- run java UDPClient <server
hostname> <port> on client before
the server is started.
- type a sentence + CR into client
- run java
UDPServer <port> on server
- What happens? Why is this different than part 4.