UDP Client Server Tutorial Java Image

UDP Client Server Tutorial Java

In this tutorial we’ll be looking at how we can set up a UDP client and server using Java’s DatagramSocket’s.

UDP Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.tutorialedge.server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import org.apache.log4j.Logger;

import com.tutorialedge.driver.ServerDriver;

public class Server implements Runnable{
	final static Logger logger = Logger.getLogger(Server.class);
	
	private DatagramSocket serverSocket;
	
	private byte[] in;
	private byte[] out;
	
	/*
	 * Our constructor which instantiates our serverSocket
	 */
	public Server() throws SocketException{
		serverSocket = new DatagramSocket(10000);
	}

	public void run() {
		while(true){
			try {
				in = new byte[1024];
				out = new byte[1024];
				
				/*
				 * Create our inbound datagram packet
				 */
				DatagramPacket receivedPacket = new DatagramPacket(in, in.length);
				serverSocket.receive(receivedPacket);
				
				/*
				 * Get the data from the packet we've just received
				 * and transform it to uppercase.
				 */
				String text = new String(receivedPacket.getData());
				out = text.toUpperCase().getBytes();
				logger.info("String Received: " + text);
				
				/*
				 * Retrieve the IP Address and port number of the datagram packet
				 * we've just received
				 */
				InetAddress IPAddress = receivedPacket.getAddress();
				int port = receivedPacket.getPort();
				
				/*
				 * Create a DatagramPacket which will return our message back to the last system
				 * that we received from
				 */
				DatagramPacket sendPacket = new DatagramPacket(in, in.length, IPAddress, port);
				serverSocket.send(sendPacket);
			} catch (IOException e) {
				/*
				 * Handle our servers exception
				 */
				logger.info("Exception thrown: " + e.getLocalizedMessage());
			}
			
		}
	}
	
	
	
}

UDP Client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.tutorialedge.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;

import com.tutorialedge.server.Server;

public class Client implements Runnable{
	final static Logger logger = Logger.getLogger(Server.class);
	
	private BufferedReader inFromUser;
	private DatagramSocket clientSocket;
	private InetAddress IPAddress;
	
	/*
	 * Our byte arrays that we'll use to read in and send out to our UDP server
	 */
	private byte[] outData;
    private byte[] inData;
	
    /*
     * Our Client constructor which instantiates our clientSocket
     * and get's our IPAddress
     */
	public Client() throws SocketException, UnknownHostException{
		clientSocket = new DatagramSocket();
		IPAddress = InetAddress.getByName("localhost");
		inFromUser = new BufferedReader(new InputStreamReader(System.in));
	}
	
	private void shutdown(){
		clientSocket.close();
	}
	
	public void run() {
		logger.info("Client Started, Listening for Input:");
		/*
		 * Start a while loop that will run until we kill the program, this will continuously
		 * poll for user input and send it to the server.
		 */
		while(true){
			try {
				inData = new byte[1024];
				outData = new byte[1024];
				/*
				 * First we read in the users input from the console.
				 */
				System.out.print("> ");
				String sentence = inFromUser.readLine();
				outData = sentence.getBytes();
				
				/*
				 * Next we create a datagram packet which will allow us send our message back to our datagram server
				 */
				DatagramPacket out = new DatagramPacket(outData, outData.length, IPAddress, 10000);
				clientSocket.send(out);
				
				/*
				 * Once we've sent our message we create a second datagram packet which will
				 * let us receive a response.
				 */
				DatagramPacket in = new DatagramPacket(inData, inData.length);
				clientSocket.receive(in);
				
				/*
				 * Finally we log the response from the server using log4j
				 */
				String modifiedSentence = new String(in.getData());
				logger.info("Server >" + modifiedSentence);
				
			} catch (IOException e) {
				/*
				 * Here we need to capture any exceptions thrown by our application
				 */
				logger.error("Exception Thrown: " + e.getLocalizedMessage());
			}
		}
	}
}

ServerDriver Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.tutorialedge.driver;

import java.net.SocketException;

import org.apache.log4j.Logger;

import com.tutorialedge.server.Server;

public class ServerDriver {
	final static Logger logger = Logger.getLogger(ServerDriver.class);
	
	public static void main(String[] args) throws SocketException{
		logger.info("Networking Tutorial v0.01");
		new Thread(new Server()).start();
	}
	
}

ClientDriver Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.tutorialedge.driver;

import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;

import com.tutorialedge.client.Client;

public class ClientDriver {
	final static Logger logger = Logger.getLogger(ServerDriver.class);
	
	public static void main(String[] args) throws SocketException, UnknownHostException{
		logger.info("Starting Client...");
		new Thread(new Client()).start();
	}
	
}
Was This Post Helpful?
Submit a PR: Edit on Github