Skip to content
UoL CS Notes

Java Socket Programming - UDP

COMP211 Lectures

A recap of the content is in Java Socket Programming Recap & Assignment 1.

A socket is a door between the application process and the transport layer. There are two socket types, one for each transport layer (UDP/TCP).

UDP

There is no explicit connection between the client and the server:

  • No handshake before data is sent.
  • Sender explicitly attache the IP destination address and port to each packet.
  • Receiver extracts the sender IP address and port from the received packet.

Transmitted data may be lost or received out of order.

UDP provides unreliable transfer of groups of bytes (data-grams) between client and server.

Example Program

The client in this example sends a string the the server over UDP. The server then converts the string to upper-case and then sends it back.

Java UDP Client

import java.io.*;
import java.net.*;

class UDPClient {
	public static void main(String args[]) throws Exception {
	
		// create input stream
		BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
		
		// create client socket
		DatagramSocket clientSocket = new DatagramSocket();
		
		// translate host name to IP address using DNS
		InetAddress IPAddress = InetAddress.getByName("hostname");
		
		byte[] sendData = net byte[1024]; // message limited to 1024 characters
		byte[] receiveData = new byte[1024];
		
		// read message from terminal
		String sentence = inFromUser.readLine();
		
		// convert string to byte array
		sendData = sentence.getBytes();
		
		// create data-gram with data to send, length, IP address and port
		DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
		
		// send data-gram to server
		clientSocket.send(sendPacket);
		
		DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
		
		// read data-gram from server
		clientSocket.receive(receivePacket);
		
		// extract data from packet
		String modifiedSentence  = new String(receivePacket.getData());
		
		System.out.println("FROM SERVER:" + modifiedSentence);
		clientSocket.close();
	}
}

Java UDP Server

import java.io.*;
import java.net.*;

class UDPServer {
	public static void main(String args[]) throws Exception {
		
		// create data-gram socket at port 9876
		DatagramSocket serverSocket = new DatagramSocket(9876);
		
		byte[] receiveData = new byte[1024];
		byte[] sendData = new byte[1024];
		
		while(true) {
			
			// create space for received data-gram
			DatagramPacket receive Packet = new DatagramPacket(receiveData, receiveData.length);
			// receive data-gram
			serverSocket.receive(receivePacket);
			
			String sentence = new String(receivePacket.getData());
			
			// get IP address and port of sender
			InetAddress IPAddress = receivePacket.getAddress();
			int port = receivePacket.getPort();
			
			String capitalisedSentence = sentence.toUpperCase();
			
			sendData = capitalisedSentence.getBytes();
			
			// create data-gram to send to client
			DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
			
			// write out data-gram to socket
			serverSocket.send(sendPacket);
		}
	}
}