Homework 5 : Programming with UDP

In this project, you will write a peer-to-peer UDP chat application with a graphical user interface and a capability for sharing photos and images among the participants. Because image files are binary files, the application will use a binary protocol.

The Chat application will allow three participants, and will support the following protocol.

  1. chat sender message: The key word chat is followed by the name of the sender followed by the message. Because this is a binary protocol, all three strings are encoded by an integer followed by the sequence of bytes that make up the string using UTF-8 encoding. This command is used to send a text message from a sender to one or both of the other participants in the chat application.
  2. image sender image-bytes : the key word image is folllowed by the name of the sender followed by the contents of an image file.
  3. bye: The key word bye is sent by itself to all participants by any one participant. This command has the effect of shutting down the entire chat session. Thus any one of the three participants can close down the entire chat session.

Examples of Screen Shots

The following are screen shots of the application showing a chat session with three individuals named Al, Bob, and Carla. An explanation of the user inteface will follow after the screen shots.

The user interface has a menu bar with two menus. One menu allows the user to specify the names of the participants and their network addresses. Originally, the user interface comes up like this:

Clicking on the participants menu brings up a dialog box that allows the user to specify the names of the participants:

Note that the dialog comes prefilled with the loopback address in the boxes for IP addresses, to allow testing by running all three instances of the application on the local machine. The port number boxes are prefilled with 50000. However, only one participant can run at that port, so the other two should use 50001 and 50002, respectively.

The Send Pic Menu is used by the local use to send an image of to one or more of the other two participants. This menu item causes a JavaFX FileChooser diaolog to pop up, which allows the user to navigate the file system and select an image to send to the selected recipients. The selected recipients are determined by checking checkboxes at the top of the GUI.

There is a sidebar at the left that shows three images. The image at the top is the last image sent by the local user. The image in the middle is the last image received from the first of the other two participant (shown on the local screen as the first recipient), while the image at the bottom is the last image received from the participant shown on the local screen as the second recipient.

There is a text area in the middle used to display the chat history in a way similar to the previous assignment. At the bottom there is a text field that is used to send text messages to the selected recipients. The message sent by the local user appears in the chat history text are, indicating to whom the message is directed. Likewise, received messages show up in the chat history text area as well, indicating from whom they came from.

A shell that creates the user interface is provided for you. You will have to add the event handlers needed to handle local menu and text field events and send messages to the remote sides, and the threads needed to monitor the network connection.

Information on UDP Programming

We will talk about this assignment in the lecture of Week of Tuesday of Week 8. You can get started by learning about UDP programming at the following links

  1. Tutorial on Java UDP Programming: This is a short introduction on how to program with UDP sockets.
  2. The DatagramPacket class: Objects of this class are used to package data to send using an UDP socket, and to receive data from a UDP socket.
  3. The DatagramSocket class: This class is used to create sockets that use UDP as the transport protocol.

Image in JavaFX

The chapter on JavaFX programming that I gave the class has a section on handling images in JavaFX. In the project shell provided, you will find a folder containing images of cars that you can use. The images in their natural sizes are a little bit too large, so they have to be shrunk a bit. The project shell has code that shows how to do this.

Final Implementation Note

The buffers you use to create DatagramPackets must be large enough to hold an image. Make them 30 KB, or 30 * 1024 bytes.

Due Date

Tuesday of Week 9.