Home » Programming Languages » JAVA Programs » UDP DatagramSocket Socket programming in JAVA (Client, Server Example )

UDP DatagramSocket Socket programming in JAVA (Client, Server Example )

In this post, we will write simple UDP Datagram client and server in JAVA language. The client sends simple string to server.

 $ vim DatagramSenderClient.java 
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class DatagramSenderClient{
        private static String remoteServerAddr = "192.168.1.102";
        private static int remoteServerPort = 5555;

        public static void main(String[] args) throws Exception {
                DatagramSocket localSocket = new DatagramSocket();

                String message = "This is string sent from DatagramSenderClient.java Client";
                InetAddress remoteServerInetAddr = InetAddress.getByName(remoteServerAddr);
                DatagramPacket dataGramPacket = new DatagramPacket(message.getBytes(), message.length(), remoteServerInetAddr, remoteServerPort);

                localSocket.send(dataGramPacket);
                localSocket.close();
        }

}

compile the client program as, [ Note: if you don’t have JAVA installed, follow “How to Install Java using apt-get command on Ubuntu” ]

 $ javac DatagramSenderClient.java 

Now, lets write a server program which will receive the data sent from the client.

 $ vim DatagramReceiverServer.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class DatagramReceiverServer {
        private static int remoteServerPort = 5555;

        public static void main(String[] args) throws Exception {

                DatagramSocket localServerSocket = new DatagramSocket(remoteServerPort);
                byte[] buf = new byte[1024];

                DatagramPacket datagramReceiverPacket = new DatagramPacket(buf, 1024);
                localServerSocket.receive(datagramReceiverPacket);

                String receivedMessage = new String(datagramReceiverPacket.getData(), 0, datagramReceiverPacket.getLength());
                System.out.println(receivedMessage);
                localServerSocket.close();
        }

}

compile the server as,

 $ javac DatagramReceiverServer.java 

Now, once client and server is successfully compiled, lets run the server program on one terminal as,

 $ java DatagramReceiverServer 

Now, open another terminal to run the client program as,

 $ java DatagramSenderClient 

once you press ENTER for above command which executes the client program to send the message to server, we will see the message printed on terminal where server was running as,

$ java DatagramReceiverServer
This is string sent from DatagramSenderClient.java Client

NOTE: if you can also run the client and server program of different Ubuntu machines.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

2 thoughts on “UDP DatagramSocket Socket programming in JAVA (Client, Server Example )”

Leave a Comment