Almighty Bus Error

Loading search...

The connectionless networking protocol

Also known as UDP, like the title suggests, it does not have a logical idea of connection.

The way it communicates is by datagrams (or messages). The protocol is not able assert any type of confirmation at a transport layer level if a message got thru to the destination. A collateral effect of this is that UDP is a very lightweight protocol, perfect for scenarios where the loss of some information does not break the intended application.

In Java, to implement UDP communication it’s mostly used the following two objects from the java.net.* package: DatagramPacket and DatagramSocket.

The DatagramPacket is used to create a datagram that will be received or sent and configure destination/port in the latter:

DatagramPacket packet = new DatagramPacket(
someString.getBytes(), someString.length() );

To configure the destination of the datagram the following methods are used:

packet.setAddress( InetAddress.getByName( someServerName ) );
packet.setPort( port );

InetAddress.getByName is an static method that given a server name like “www.google.com” returns the IP address.

A DatagramPacket can be used to send any type of message that has been converted to an array of byte. There is, however, an UDP protocol 65,507 byte limit to the size of the array.

To extract the information from a received packet the following methods are used:

byte[] info = packet.getData();
int infoLength = packet.getLength();

A DatagramSocket is initialized using the constructor that receives a port which will be used for sending and receiving datagrams:

DatagramSocket socket = new DatagramSocket( port );

The methods used by this object are:

socket.send( DatagramPacket x );
socket.receive( DatagramPacket x );

The receive method is blocking, it will block the current execution flow until it has received a message.
To receive a packet, it doesn’t make sense to configure a destination and a port, only a buffer to where the data will be stored like in the declaration of the packet object.
Since all the complex ( i.e. Object ) argument passing is by reference, the received packet will overwrite all the information in x.

For more information on UDP go here.

Hope this has been useful.

Thank you for reading!