UDP Server

As from the UDP Client here is the UDP Server, it is similar to the TCP Server in that it makes connections with a remote client and sends and receives data, but once again this data is not checked to make sure that it has been received (the “fire and forget” setup again)

So to start with we need to create a server socket for the clients to connect to

DatagramSocket theSocket = new DatagramSocket(9999);

since we now have a place for the client to connect to we now need to wait for a client connection request

// create a place for the client to send data too
theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
// wait for a client to request a connection
theSocket.receive(theRecievedPacket);

once a client has requested some data, we can start the process of setting up the response, the response starts of with getting there details, there IP address and also the port number that they wish to talk on ( image attached below of the wireshark request and different port number)

// get the client details
clientAddress = theRecievedPacket.getAddress();
clientPort = theRecievedPacket.getPort();

and then just build up the response to send back and send it

String message = "Server - client sent : " + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());
outBuffer = message.getBytes();
 
// send some data to the client
theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);
theSocket.send(theSendPacket);

here is the full java code

import java.io.*;
import java.net.*;
 
public class UDPServer {
 
	DatagramSocket theSocket = null;
	int serverPort = 9999;
 
	public UDPServer()
	{
		try {
			// create the server UDP end point
			theSocket = new DatagramSocket(serverPort);
 
			System.out.println("UDP Socket (end point) created");
		} catch (SocketException ExceSocket)
		{
			System.out.println("Socket creation error : "+ ExceSocket.getMessage());
		}
	}
 
	public void clientRequest()
	{
		DatagramPacket theRecievedPacket;
		DatagramPacket theSendPacket;
		InetAddress clientAddress;
		int clientPort;
		byte[] outBuffer;
		byte[] inBuffer;
 
		// create some space for the text to send and recieve data 
		outBuffer = new byte[500];
		inBuffer = new byte[50];
 
		try {
			// create a place for the client to send data too
			theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
			// wait for a client to request a connection
			theSocket.receive(theRecievedPacket);
			System.out.println("Client connected");
 
			// get the client details
			clientAddress = theRecievedPacket.getAddress();
			clientPort = theRecievedPacket.getPort();
 
			String message = "Server - client sent : " + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());
			outBuffer = message.getBytes();
 
			System.out.println("Client data sent ("+message+")");
			// send some data to the client
			theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);
			theSocket.send(theSendPacket);
 
		} catch (IOException ExceIO)
		{
			System.out.println("Error with client request : "+ExceIO.getMessage());
		}
		// close the server socket
		theSocket.close();
	}
 
	public static void main(String[] args)
	{
		UDPServer theServer = new UDPServer();
		theServer.clientRequest();
	}
}

if you save that as UDPServer.java and then compile and run

javac UDPServer.java

and then run the java UDPserver program, I have inserted the “— waiting for the client to connect” line because that is where the server will stop waiting, once the client as connected the rest of the output will be outputted.

java UDPServer 
UDP Socket (end point) created
---- waiting for the client to connect
Client connected
Client data sent (Server - client sent : genux)

and then run the UDPClient to connect to the server

java UDPClient 
Client socket created
Message sending is : genux
Client - server response : Server - client sent : genux

Here is the image of a wireshark connection request over the UDP and with the clients port request number.

Request port number
Request port number

UDP Client

A UDP Client is very similar to a TCP Client in that it communicates with a server, but the main difference between the two is that UDP is more of a “fire and forget” setup than lets make sure that the data is sent across. A good example of this would be for a UDP server/client to be something like a radio station, when you are listening to the radio and if you miss part of the song then it does not really matter (apart from if you was singing along!!), but with TCP server/client you want to have all of the data associated with that file.

// create a socket to connect to the server with
DatagramSocket theSocket = new DatagramSocket();
int serverPort = 9999;
// the remote server, in this case I am using the local host but you could use any remote server as long as the UDP server is running on it
InetAddress theServer = InetAddress.getLocalHost();
// and connect the socket to the remote server and port number
theSocket.connect(theServer,serverPort);

and now we build up a message to send to the server, we are using bytes because that is what is sent over with the DatagramPacket’s (these are the packets of data within the wrapped up “letter” that is being sent from the client to the server, the “letter” in this case is the details included within the packet e.g. address to, address from more details are here)

byte[] outBuffer = new byte[50];
String message = "genux";
outBuffer = message.getBytes();

and then to send the information to the server we, since theSocket is already connected to the server then do not need to tell the DatagramPacket where to send.

// build up a packet to send to the server
theSendPacket = new DatagramPacket(outBuffer, outBuffer.length);
// send the data
theSocket.send(theSendPacket);

and now to get a response (if the server gets, and the client does receive one)

// get the servers response within this packet
theReceivedPacket = new DatagramPacket(inBuffer, inBuffer.length);
theSocket.receive(theReceivedPacket);
 
// the server response is...
System.out.println("Client - server response : "+new String(theReceivedPacket.getData(), 0, theReceivedPacket.getLength()));

here is the full code

import java.io.*;
import java.net.*;
 
public class UDPClient {
 
	DatagramSocket theSocket = null;
	int serverPort = 9999;
 
	public UDPClient()
	{
		try {
			theSocket = new DatagramSocket();
 
			// but if you want to connect to your remote server, then alter the theServer address below
			InetAddress theServer = InetAddress.getLocalHost();
			theSocket.connect(theServer,serverPort);
 
			System.out.println("Client socket created");
		}catch (SocketException ExceSocket)
		{
			System.out.println("Socket creation error  : "+ExceSocket.getMessage());
		} 
		catch (UnknownHostException ExceHost)
		{
			System.out.println("Socket host unknown : "+ExceHost.getMessage());
		}
	}
 
	public void connectToServer()
	{
		DatagramPacket theSendPacket;
		DatagramPacket theReceivedPacket;
		InetAddress theServerAddress;
		byte[] outBuffer;
		byte[] inBuffer;
 
		// the place to store the sending and receiving data
		inBuffer = new byte[500];
		outBuffer = new byte[50];
		try {
			String message = "genux";
			outBuffer = message.getBytes();
 
			System.out.println("Message sending is : " + message);
 
			// the server details
			theServerAddress = theSocket.getLocalAddress();
 
			// build up a packet to send to the server
			theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, theServerAddress, serverPort);
			// send the data
			theSocket.send(theSendPacket);
 
			// get the servers response within this packet
			theReceivedPacket = new DatagramPacket(inBuffer, inBuffer.length);
			theSocket.receive(theReceivedPacket);
 
			// the server response is...
			System.out.println("Client - server response : "+new String(theReceivedPacket.getData(), 0, theReceivedPacket.getLength()));
			theSocket.close();
		} catch (IOException ExceIO)
		{
			System.out.println("Client getting data error : "+ExceIO.getMessage());
		}
	}
 
	public static void main(String[] args)
	{
		UDPClient theClient = new UDPClient();
		theClient.connectToServer();
	}
}

if you save that as UDPClient.java and then to compile

javac UDPClient.java

the server is the next part of the puzzle, which is coming next.