Further Java Practical Class
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 format. An example client is shown in Figure 1, “An example image chat client”.
Your program should have the following features:
Your solution should contain a class called
ImageChatClient which extends
JFrame inside a package called
Your implementation of
ImageChatClient must contain a constructor which takes two arguments; the first argument should accept the name of the server as a
String and the second argument the port number to connect to, expressed as an
When an object of type
ImageChatClient is instantiated it should create a Swing GUI with at least two components, an AWT
Canvas object to render images sent by the server, and an AWT
Button with the text label "Upload". You may add additional GUI components if you wish.
Your implementation of
ImageChatClient should use an additional thread in a similar manner to
StringChat you wrote for Ticklet 1. One thread should listen on a
Socket object connected to the server and draw any new image sent by the server to the
Canvas object; the other thread should listen for user input.
Button object is clicked, you should create and show an instance of the
JFileChooser class. In the case where a user selects a suitable JPEG file using the dialog and selects the "Open" button, your program should send the bytes representing the JPEG file to the server over a
Socket object; otherwise your program should do nothing.
Clicking on the close button in the top right-hand corner of the Swing window created by the
ImageChatClient class should close the application.
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/0809/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
-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
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
firstname.lastname@example.org. You should receive a response via email within an hour. If you do not, please send an email to