Simple Calculator via UDP in Java

The Internet Protocol suite contains all sort of protocols that enables communication between devices over the Internet. UDP is one of the protocols of this suite and its full form is User Datagram Protocol. Unlike TCP, it is not reliable and also it is a connectionless protocol. It does not establish any kind of connection to other devices before sending the data.

In this article, we will develop a simple client-server side calculator using UDP in Java. The client will request the operation and the server will send the result to client device after calculating it.

Java Networking

Let’s briefly understand a few basic concepts first about Java networking −


An IP address is either a 32-bit or 128-bit unsigned number that uniquely identifies devices on Internet. It is easy to remember the name of IP host rather than the numerical address. So, we need to encapsulate this using ‘InetAddress’ class. We use its built-in method ‘getLcalHost()’ to retrieve the IP address of LocalHost.


They are small-sized packets that contain data that can be passed between two machines over the internet. Java implements two classes to establish UDP connection −

DatagramSocket class is used to send and receive datagram packets. It also determines the destination of these packets. Its built-in methods ‘send()’ and ‘receive()’ are used to send and receive the packets respectively.


DatagramSocket nameOfObject = new DatagramSocket();   

DatagramPacket class’s object stores the data to be sent.


DatagramPacket nameOfPacket = new DatagramPacket();   

Program of Calculator using UDP

Program of Client Side

Working of code

  • We first import the two most important packages named ‘’ to access all the classes regarding Java networking and ‘’ for input and output streams. The ‘java.util’ package to use ‘Scanner’ class.

  • Make a UDP connection and then, fetch the LocalHost address.

  • Now, inside the try block, we will ask for user input to request an operation and to receive the result accordingly. It will be done by ‘send()’ and ‘receive()’ methods of ‘DatagramSocket’ class.

import java.util.*;
public class ClientCalc {
   public static void main(String args[]) throws IOException {
      Scanner inp = new Scanner(;
      // making UDP connection
      DatagramSocket datagsokt = new DatagramSocket();   
      // fetching the localhost address
      InetAddress addr = InetAddress.getLocalHost();
      byte strm[] = null;
      try {
         while (true) {
            System.out.println("Type 1 for Addition");
            System.out.println("Type 2 for Subtraction");
            System.out.println("Type 3 for Multiplication");
            System.out.println("Type 4 for Division");
            System.out.println("Enter your choice: ");
            String oprtr = inp.nextLine();
            // to convert the user choice to byte
            strm = new byte[256];
            strm = oprtr.getBytes();
            // creating datagram packet to send to server
            DatagramPacket packtsend = new DatagramPacket(strm, strm.length, addr, 6666);
            // Type 0 for cut the connection
            if (oprtr.equals("0")) {
            // to receive the result from server
            strm = new byte[256];
            DatagramPacket packtrec = new DatagramPacket(strm,   strm.length);
            // display the result 
            System.out.println("Your Result for the given operation = " + new String(strm, 0, strm.length));
      // to handle exception
      catch(Exception exp) {

Program of Server Side

Working of code

  • First, establish a connection with the client and define two objects of DatagramPacket class to send and receive the packet using ‘send()’ and ‘receive()’ methods of ‘DatagramSocket’ class.

  • Inside the try block, we receive the request from client and then, using switch case we perform the operation and send the result to client device.


class ServerCalc {
   public static void main(String[] args) throws IOException {
      // making connection to client
      DatagramSocket datagsokt = new DatagramSocket(6666);
      byte[] strm = null;
      DatagramPacket packtrec = null;
      DatagramPacket packtsend = null;
      try {
         while (true) {
            strm = new byte[256];
            // to receive the packet from client
            packtrec = new DatagramPacket(strm, strm.length);
            String oprtr = new String(strm, 0, strm.length);
            System.out.println("Client has requested for " + oprtr );
            int data1 = 15;
            int data2 = 5;
            int tot = 0;
            char opt = oprtr.charAt(0); 
            switch(opt) {
               case '1' : 
                  tot = data1 + data2;
               case '2' :
                  tot = data1 - data2;  
               case '3' :
                  tot = data1 * data2;
               case '4' :
                  tot = data1 / data2;  
               default :
            // Converting the string result to integer 
            String res = Integer.toString(tot);
            // converting the integer to bytes
            strm = res.getBytes();
            int port = packtrec.getPort(); 
            // getting port number
            // sending final result in the form of datagram packet
            packtsend = new DatagramPacket(strm, strm.length, InetAddress.getLocalHost(), port);
      // to handle exception
      catch(Exception exp) {

To run both programs, open two cmd simultaneously on your local machine. On first cmd interface, compile and run the server-side program and then on the other interface, execute the client-side program.

Output on Client side

D:\Java Programs>java ClientCalc
Type 1 for Addition
Type 2 for Subtraction
Type 3 for Multiplication
Type 4 for Division
Enter your choice:
Your Result for the given operation = 20  

Output on Server side

D:\Java Programs>java ServerCalc
Client has requested for 1  

When we enter 0 the connection will get terminated and program will stop its execution.


In this article, we have learned about a few essential concepts of Java networking. Also, we discussed server-side and client-side programs of a simple calculator using UDP. We discovered how we could establish a connection between client and server devices in Java.

Updated on: 16-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started