UDP server and UDP client in java

Example: send and receive between udp server and upd client

1.create upd server with port 8000:

 

package com.mycompany.testmqtts;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

public class UDPServer {

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

 

        //start udp server with port 8000

        DatagramSocket socket = new DatagramSocket(8000);

        System.out.println("server is running");

        //byte for data in out

        byte[] inServer = new byte[1024];

        byte[] outServer = new byte[1024];

        //create package receive data from client

        DatagramPacket rcvPkt = new DatagramPacket(inServer, inServer.length);

        while (true) {

            // waiting receive data from client

            socket.receive(rcvPkt);

            System.out.println("Packet Received!");

            System.out.println("ip Address!" + rcvPkt.getAddress());

            System.out.println("port!" + rcvPkt.getPort());

            System.out.println("message Received!" + new String(rcvPkt.getData()));

            InetAddress IP = rcvPkt.getAddress();

            int port = rcvPkt.getPort();

            //receive data

            String temp = new String(rcvPkt.getData());

            temp = "server :" + temp.toUpperCase();

            outServer = temp.getBytes();

            //send message response for client

            DatagramPacket sndPkt = new DatagramPacket(outServer, outServer.length, IP, port);

            socket.send(sndPkt);

        }

    }

}

 

2. udp client send message for upd server

 

package com.mycompany.testmqtts;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

public class UDPClient {

    public static void main(String args[]) {

        try {

            //create connect with udp server

            DatagramSocket socket = new DatagramSocket();

            //byte data in out

            byte[] inData = new byte[1024];

            byte[] outData = new byte[1024];

            //ip or hostname of udp server

            InetAddress IP = InetAddress.getByName("localhost");

            //message send to udp server

            String data = "hello kaka";

     outData = data.getBytes();

            //send message

            DatagramPacket sendPkt = new DatagramPacket(outData, outData.length, IP, 8000);

            System.out.println("ready connect server");

            socket.send(sendPkt);

            socket.setSoTimeout(10000);

            System.out.println("connect server success");

            //waiting response of udp erver

            DatagramPacket recievePkt = new DatagramPacket(inData, inData.length);

            System.out.println("ready receive message from server)");

            socket.receive(recievePkt);

            System.out.println("receive messag");

            System.out.println("Replay from Server: " + new String(recievePkt.getData()));

        } catch (Exception e) {

            System.out.println("error connect udp server");

        }

    }

}

 

3.Run UDPServer  then UDPClient  for result:

Server:

 

server is running

Packet Received!

ip Address!/127.0.0.1

port!61703

message Received!hello kaka

 

Client:

 

ready connect server

connect server success

ready receive message from server)

receive message

Replay from Server: server :HELLO KAKA