Sender and Receiver of Simulated UDP Transport

Keywords: Java socket

1. Create the sender of UDP transmission

1. Establishing UDP Socket service;
2. The data to be sent is encapsulated in the data package.
3. Send the data package through UDP's Socket service.
4. Close the Socket service.

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

public class UDPSend {

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

        System.out.println("Sender Start......");

        // 1. Create UDP Socket and use DatagramSocket object
        DatagramSocket ds = new DatagramSocket();

        // 2. Encapsulating the data to be sent into the data package
        String str = "UDP Transmission demonstration: I'm coming!";

        byte[] buf = str.getBytes();  //Encapsulating data into the package of the object using Datagram Packet

        DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);

        // 3. Send the data package through the UDP Socket service, using send method
        ds.send(dp);

        // 4. Close Socket Service
        ds.close();
    }
}
  • 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
  • 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

2. Creating the Receiver of UDP Transport

1. Establish UDP Socket service, because to receive data, it is necessary to specify a port number;
2. Create data packages to store received data, which can be easily parsed by means of data package objects.
3. receive data using UDP Socket service and store it in data package.
4. Analyzing these data by means of data package;
5. Close the Socket service.

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

public class UDPReceive {
    public static void main(String[] args) throws IOException {

        System.out.println("Receiver Start......");

        // 1. Establishing UDP Socket Service
        DatagramSocket ds = new DatagramSocket(10000);

        // 2. Creating Packets
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf, buf.length);

        // 3. Store data in data packages using receiving methods
        ds.receive(dp);  // The method is blocking

        // 4. Resolve these data by means of data package object, such as address, port, data content, etc.
        String ip = dp.getAddress().getHostAddress();
        int port = dp.getPort();
        String text = new String(dp.getData(), 0, dp.getLength());

        System.out.println(ip + ":" + port + ":" + text);

        // 5. Close Socket Service
        ds.close();
    }
}
  • 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
  • 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

3. Optimizing the sender and receiver of UDP transmission

Because in the first two parts, we can only send (or receive) one message at a time, and then shut down the service! Therefore, if we want to send more than one message, we need to constantly modify the content sent at the sender, and also need to restart the server, which is more troublesome. In order to overcome the above shortcomings, we can optimize it, namely:
1. Create BufferedReader on the sender and input content from the keyboard;
2. At the receiving end, add while(ture) loop to receive content continuously.

/**
*Optimizing the sender of UDP transmission
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSend {
    public static void main(String[] args) throws IOException {

        System.out.println("Sender Start......");

        // Create UDP Socket, using DatagramSocket objects
        DatagramSocket ds = new DatagramSocket();

        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line = bufr.readLine()) != null) {
            // Encapsulating data into the package of the object using Datagram Packet
            byte[] buf = line.getBytes();
            DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);
            // Send the data package through the UDP Socket service, using the send method
            ds.send(dp);
            // If the input information is over, the loop ends
            if ("over".equals(line))
                break;
        }
        // Close Socket Service
        ds.close();
    }
}
  • 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
  • 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
/**
*Optimizing the Receiver of UDP Transmission
*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceive {
    public static void main(String[] args) throws IOException {

        System.out.println("Receiver Start......");

        // Establishing UDP Socket Service
        DatagramSocket ds = new DatagramSocket(10000);

        while(true) {
            // Creating Packets
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf, buf.length);

            // Store data in data packages using receiving methods
            ds.receive(dp);  // The method is blocking

            // Resolve these data by means of data package objects, such as address, port, data content, etc.
            String ip = dp.getAddress().getHostAddress();

            int port = dp.getPort();
            String text = new String(dp.getData(), 0, dp.getLength());
            System.out.println(ip + ":" + port + ":" + text);
        }
    }
}
  • 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
  • 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

IV. Creating Chat Room

According to the relevant properties of UDP (User Datagram Protocol), we can further create a simple chat room based on UDP transmission protocol to realize the function of interactive chat.

/**
*Create chat room sender under UDP transmission
*/
package chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Send implements Runnable {

    private DatagramSocket ds;

    public Send(DatagramSocket ds) {
        this.ds = ds;
    }

    public void run() {
        try {
            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
            String line = null;
            while ((line = bufr.readLine()) != null) {
                byte[] buf = line.getBytes();
                DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.255"), 10001);
                ds.send(dp);
                if ("886".equals(line))
                    break;
            }
            ds.close();
        } catch (Exception e) {
            System.out.println("Sorry, there was a mistake!");
        }
    }
}
  • 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
  • 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
/**
*Create a chat room receiver under UDP transmission
*/
package chat;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Rece implements Runnable {

    private DatagramSocket ds;

    public Rece(DatagramSocket ds) {
        this.ds = ds;
    }

    public void run() {
        try {
            while (true) {
                byte[] buf = new byte[1024];
                DatagramPacket dp = new DatagramPacket(buf, buf.length);
                ds.receive(dp);
                String ip = dp.getAddress().getHostAddress();
                String text = new String(dp.getData(), 0, dp.getLength());
                System.out.println(ip + ":::" + text);
                if(text.equals("886")){
                    System.out.println(ip+"......Get out of the chat room!");
                }
            }
        } catch (Exception e) {
            System.out.println("Sorry, there was a mistake!");
        }
    }
}
  • 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
  • 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
/**
*Create chat rooms under UDP transmission
*/
package chat;

import java.io.IOException;
import java.net.DatagramSocket;

public class ChatRoom {
    public static void main(String[] args) throws IOException {
        DatagramSocket send = new DatagramSocket();
        DatagramSocket rece = new DatagramSocket(10001);
        new Thread(new Send(send)).start();
        new Thread(new Rece(rece)).start();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Posted by nahla123 on Wed, 17 Jul 2019 14:08:27 -0700