/ Programming Ib: Lab
Concurrency and Networking: Concurrency Meets Networking
Back to contents
In this section we are going to show you how to improve your
and your server using threads. If you read ahead on the Oracle
tutorial, then you will have seen that in order to support multiple
clients in Java you will need threads on the server side. This is true!
It may be less obvious that you also need threads on the client side,
but you do. This is so you can receive an unspecified number of lines
from the server much more easily. As you are about to find out.
Run your client with the new server. What do you notice? Chances are
receive one line, i.e. one character, of the response. The reason for
this is because you only read one line from the server for every line
that is sent. Now, you might be tempted to put the call to a read line from
the server in a loop and just read as many as it sends back. However,
the method call to read a line will actually wait for a response.
Therefore if you try to read lines until the server stops
sending them, you will find that your program will hang, as eventually
it will stop but your program will keep waiting.
your EchoServer from the last section so that it sends back the
response with just one
character per line.
In other words, if you send the line
To the server, you should receive
The way we will solve this is to introduce threads into the client. The
idea is as follows:
- Main Thread: the main thread of the application will
the server. It will then spawn a 'Listen Thread'. Then it will take
input from the user, and send it to the server. Then go back to taking
input from the user and loop between these two actions (it does not try to read from the server
- Listen Thread: the listen thread will read a line from
server, and immediately print it to the command line. Then it will go
back to listening to the server and loop.
- Implement the Listen Thread (using whichever thread style you like) and modify
EchoClient so that now it communicates with the server properly.
Now your client can listen to the server and send more lines
same time, no matter how many lines the server sends or how much input
the user gives.
If you haven't already, read the final section of the
networking tutorial at the bottom of the page called Supporting
The basic idea with multiple clients is that on the server side your
main thread listens for new connections, creates a thread to deal with
it when one comes in, and then goes back to listening for new
- Using the KKMultiServer for inspiration (if you wish),
EchoServer so that it can support multiple clients. Every client that
connects should be able to send a message to the server, and have it
returned on multiple lines.
Now you should have two threaded applications, one client and
server. The server should be able to support multiple clients which
should be able to support multiple lines coming back form the server.
It's time to move to the chat application and the final stage of this
sheet.Chat application >>
page author: Andrew Chinery
last updated: 01 March 2013