Further Java Practical Class

Further Java Ticklet 1*

Table of Contents

Hints 'n' tips

In order to gain a star in the mark sheet you must complete this exercise. Completing the exercise does not gain you any credit in the examination. In this exercise you will write a client which connects to a server and sends and receive images in JPEG[1] format. An example client is shown in Figure 1, “An example image chat client”.

Screenshot: An example image chat client

Figure 1. An example image chat client

Your program should have the following features:

Hints 'n' tips

  • You may test your implementation by connecting to java-1b.cl.cam.ac.uk on port number 15002.

  • You may wish to look at the workbooks from last year to refresh your knowledge of the JFileChooser class (http://www.cl.cam.ac.uk/teaching/0910/ProgJava/).

  • The data sent by the server is a sequence of JPEG images sent as a stream of bytes. You will need to design a method of working out when you have received all the data representing a single image. One solution is to inspect the stream for the JPEG format End Of Image (EOI) marker, which in hexadecimal is 0xffD9. (In your code you can check whether consecutive bytes have the values -1 and -39 to detect the end of an image in the stream of bytes; why?)

  • Avoid using the ImageIO.read(InputStream is) method to directly read data from a Socket object since the read method (erroneously in this case!) assumes that only a single image exists in a given stream of data. Instead you should read the data from the Socket object into a small buffer, perform some manual checks to detect whether you have reached the end of the JPEG image or not (e.g. look for the EOI marker), before writing the data out to a second stream which you can feed into ImageIO.read(InputStream is). You will probably find a small array of type byte and the ByteArrayInputStream and ByteArrayOutputStream classes useful for this.


Please put the source code and byte code of ImageChatClient, together with any other classes you wrote to support your application, into a jar file called crsid-tick1star.jar. Please email the jar file to ticks1b-java@cl.cam.ac.uk. You should receive a response via email within an hour. If you do not, please send an email to ticks1b-admin@cl.cam.ac.uk.

Copyright Alastair R. Beresford and Andrew C. Rice 2008,2009