[Learning JAVA from scratch | Article 43] Three communication methods of UDP

Table of contents

Foreword:

UDP three communication methods: 

Unicast:

Multicast: 

broadcast: 

Application scenarios of the three communication methods:

Summarize:


Foreword:

        When we first learned network programming, we introduced two extremely important protocols, namely the TCP protocol and the UDP protocol. Today we will introduce the three communication methods of the UDP protocol.

UDP three communication methods: 

Unicast:

Unicast : Unicast refers tothe communication method of transmitting data from one sender to one receiver . In UDP, unicast is the most common communication method. The sender sends data directly to the receiver by specifying the receiver's IP address and port number.

import java.net.*;

public class UnicastSender {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket();
        String message = "This is a unicast message";
        InetAddress receiverAddress = InetAddress.getByName("192.168.0.100");
        int receiverPort = 12345;
        byte[] buffer = message.getBytes();
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, receiverAddress, receiverPort);
        socket.send(packet);
        socket.close();
    }
}

public class UnicastReceiver {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(12345);
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        socket.receive(packet);
        String message = new String(packet.getData(), 0, packet.getLength());
        System.out.println("Received unicast message: " + message);
        socket.close();
    }
}

Multicast: 

Multicast : Multicast refers toa communication method that transmits data from one sender to a group of receivers . A sender sends data to a specific multicast group address shared by a group of receivers. The receiver only needs to join the multicast group to receive the data sent by the sender. Multicast is suitable for scenarios where the same data needs to be sent to a group of receivers, such as live video and multiplayer games.

import java.net.*;

public class MulticastSender {
    public static void main(String[] args) throws Exception {
        MulticastSocket socket = new MulticastSocket();
        String message = "This is a multicast message";
        InetAddress multicastAddress = InetAddress.getByName("224.0.0.1");
        int multicastPort = 12345;
        byte[] buffer = message.getBytes();
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, multicastPort);
        socket.send(packet);
        socket.close();
    }
}

public class MulticastReceiver {
    public static void main(String[] args) throws Exception {
        InetAddress multicastAddress = InetAddress.getByName("224.0.0.1");
        int multicastPort = 12345;
        MulticastSocket socket = new MulticastSocket(multicastPort);
        socket.joinGroup(multicastAddress);

        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        socket.receive(packet);
        String message = new String(packet.getData(), 0, packet.getLength());
        System.out.println("Received multicast message: " + message);

        socket.leaveGroup(multicastAddress);
        socket.close();
    }
}

broadcast: 

Broadcast : Broadcasting refers tothe communication method that transmits data from one sender to all receivers in the network . The data sent by the sender is copied to all nodes in the network, and all receivers can receive this data. Broadcasting is suitable for scenarios where the same data needs to be sent to all devices in the network, such as network management and device discovery. It is important to note that in modern networks, broadcasts are somewhat restricted and controlled to prevent abuse and network congestion.

import java.net.*;

public class BroadcastSender {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket();
        String message = "This is a broadcast message";
        InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255");
        int broadcastPort = 12345;
        socket.setBroadcast(true);
        byte[] buffer = message.getBytes();
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, broadcastAddress, broadcastPort);
        socket.send(packet);
        socket.close();
    }
}

public class BroadcastReceiver {
    public static void main(String[] args) throws Exception {
        int broadcastPort = 12345;
        DatagramSocket socket = new DatagramSocket(broadcastPort);

        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        socket.receive(packet);
        String message = new String(packet.getData(), 0, packet.getLength());
        System.out.println("Received broadcast message: " + message);

        socket.close();
    }
}

Application scenarios of the three communication methods:

  1. Unicast (Unicast):

    • Peer-to-Peer Communication: Unicast is the most common form of communication when data needs to be transmitted from a sender to a specific receiver. For example, scenarios such as file transfers, web requests, and one-to-one video calls can all use unicast communication.
  2. Multicast:

    • Multi-person real-time communication: Multicast is suitable for scenarios where the same data needs to be sent to a group of recipients. For example, video conferencing, multiplayer games, real-time audio broadcasting, etc. By using multicast, the sender only needs to send data to a specific multicast group address instead of sending it to each receiver individually.
  3. Broadcast:

    • Network management: Broadcasting is often used for network management and device discovery, such as finding available servers or devices in the local network.
    • Network Broadcasting: Broadcasting is suitable for sending the same data to all devices in the network. For example, webcasts can be used to send important notifications, emergency alerts, etc. to all devices.

It should be noted that with the increasing importance of network security, the use of multicast and broadcast may be subject to some restrictions and controls to prevent abuse and network congestion. In modern networks, multicast and broadcast features need to be used with caution and in compliance with network management regulations and best practices.

Summarize:

        In this article, we introduce three communication methods of UDP, which have different application scenarios. We need to understand the use and characteristics of these three communication methods.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

 

        

Guess you like

Origin blog.csdn.net/fckbb/article/details/132197268