Java Whiteboard - Class DatagramSocket
The purpose of this assignment was to:
- Learn about the P2P paradigm/model.
- Learn about UDP.
- Learn how to use low-level connections with datagram sockets(Java's class DatagramSocket).
The description and my solution of the Java Whiteboard -assignment can be viewed and/or downloaded at the bottom of this page.
What is P2P?
P2P(Peer-To-Peer) is a term for describing a paradigm/model in which there is no notion of clients or servers, but only equal peers that simultaneously function as both "clients" and "servers".
In the P2P process, peers trade information between each other directly without the assistance of a third party. That is, they have equal status and control.
In short: In a P2P enviroment, peers acts as both servers and clients.
Class DatagramSocket - a Low-level networking interface
Class DatagramSocket represents a socket for sending and receiving datagram packets over a network using the UDP protocol.
A datagram is a low-level networking interface - simply an array of bytes - that doesn't implement any kind of stream-based communication protocol, i.e. there is no connection(connection-less) established between the sender and the receiver before data are sent.
Datagram packets are called "unreliable" because the protocol used - UDP - does not ensure that the datagram sent arrives at the receiver's end, and doesn't resend them if they didn't. Hence, packets sent through a DatagramSocket are not guaranteed to arrive in the order sent, or to arrive at all.
This low-overhead protocol, however, makes packet transmission very fast.
Useful properties of class DatagramSocket
If a port is specified when the DatagramSocket is first created, that port will be used. If no port is specified the system will assign a default port.
- The method getLocalPort( ) returns the port number that a datagram socket uses.
- Method send( ) simply sends a DatagramPacket through the socket. (The packet must contain the destination address to which it should be sent)
- Method receive( ) waits for data to arrive at the socket and - when it does - stores it along with the address of the sender in the specified datagram packet.
- Method close( ) closes the socket and frees the port in use. Once method close( ) has been called, the same DatagramSocket shouldn't be used again.
More on Datagram Sockets and UDP
Datagram Sockets use the UDP, which - unlike TCP - is a very quick and unreliable technique for sending data over the IP.
When you send UDP data you have no way of knowing whether it arrived, much less whether different pieces of data arrived in the order in which your sent them.
So why use UDP?. Well, as already mentioned, it's fast! The pieces that do arrive, arrive much faster than with TCP. This makes the use of datagram sockets the perfect choice for sending data when speed is more important than reliable transmission.
Assignment Description
Create a Java application that implements a distributed whiteboard. Everything drawn should be sent to, and displayed by, another identical application. The other application should work the same way. Make use of Java's class DatagramSocket.
Here the use of UDP with datagram sockets is the obvious choice since the communication need to be fast. If some datagram with the location coordinates of a dot on the whiteboard doesn't arrive it doesn't matter as much as if every datagram was delayed.
This Java whiteboard application doesn't have a very fancy GUI. It's simply an example to demonstrate the benefits of using UDP with datagram sockets when transmission speed is of importance.
