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.

2 thoughts on “UDP Client”

  1. I’m not convinced, but where you try to get server details (theServerAdress = theSocket.getLocalAdress() ) shouldn’t be actually theServerAdress = InetAdress.getLocalHost() ?

Leave a Reply

Your email address will not be published. Required fields are marked *