Java broadcast datagram


Broadcast Datagram

  • This communication is similar to a radio broadcast, as long as the user specified channel upwards to be able to listen to the broadcast content. To achieve this, we need to use a special IP address. To achieve the multicast or broadcast communication host must join the same class D address. Decimal representation Class D address range 224.0.0.0 ~ 239.255.255.255;
  • To achieve the radio, you need to use MulticastSocket class. This class is based on DatagramSocket UDP for transmitting and receiving an IP multicast packet. Objects of that class can add other multicast hosts "group" on the Internet. DatagramSocket class common method as follows:
    (1) MulticastSocket(int port) throws IOException: Create a multicast socket and bind it to a port;
    (2) MulticastSocket(SocketAddress bindaddr) throws IOException: Create a multicast socket and bind it to a specified set of connected to the word address;
    (3) public void joinGroup(InetAddress mcastaddr) throws IOException: the multicast socket to join the specified multicast group;
    (4) public void leaveGroup(InetAddress mcastaddr) throws IOException: the multicast socket out multicast group;
    (5) public void setTimeToLive(int ttl) throws IOException: set the multicast data sent on this MulticastSocket the default package survival time;
  • java.net.MulticastSocket,详见:Class MulticastSocket
  • ttl parameter setting the maximum number of data packets across the network may be:
    • When ttl is 0, the packet should remain in the local host;
    • When ttl is 1, the packet should be sent to the local area network;
    • When ttl 32, the packet should be sent to the site on the network;
    • When ttl 64, the packet should be retained in the region;
    • When ttl 128, the packet should be retained in the continent;
      - it is when ttl 255, packets can be sent to all places;

1. Application: Broadcast Datagram

a. transmitting end

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

class BroadCast {
    public void send() {
        String msg = "Hello,This is Broadcast Message"; // 多播的内容
        int port = 6666; // 多播端口号
        InetAddress group = null;
        MulticastSocket ms = null;
        try {
            group = InetAddress.getByName("224.1.1.1"); // 创建多播地址
            ms = new MulticastSocket(port); // 创建多播套接字
            ms.joinGroup(group); // 将套接字加入多播地址
            ms.setTimeToLive(1); // 设置数据报发送范围为本地
            DatagramPacket dp = new DatagramPacket(msg.getBytes(), msg.length(), group, port);
            // 创建待发送的数据报
            ms.send(dp); // 发送数据报
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            ms.close(); // 关闭套接字
        }}}
public class BroadcastTest {
    public static void main(String[] args) {
        new BroadCast().send();
    }
}

b. the receiving end

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

class Receiver {
    public void receive() {
        byte[] data = new byte[1024]; // 数据缓存区
        int port = 6666; // 多播端口号
        InetAddress group = null;
        MulticastSocket ms = null;
        try {
            group = InetAddress.getByName("224.1.1.1"); // 创建多播地址
            ms = new MulticastSocket(port); // 创建多播套接字
            ms.joinGroup(group); // 将套接字加入多播地址
            DatagramPacket dp = new DatagramPacket(data, data.length, group, port);
            // 创建待接收的数据报
            ms.receive(dp); // 接收数据报
            String msg = new String(dp.getData(), 0, dp.getLength());
            System.out.println("接收的广播数据为:" + msg);
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            ms.close(); // 关闭套接字
        }
    }
}

public class ReceiverTest {
    public static void main(String[] args) {
        new Receiver().receive();
    }
}

c. Run the demo

  • First operation receiving terminal, and then ends the broadcast operation, sends a broadcast message, the client will receive the message that is displayed:
    Here Insert Picture Description
Published 279 original articles · won praise 283 · views 10000 +

Guess you like

Origin blog.csdn.net/Regino/article/details/105035955