Socket多接收者

通常服务端与客户端是一对一的通信方法(也叫单播),一对多服务(广播与多播),只有UDP Socket允许广播与多播.
广播:(本地)网络中的所有主机都会接收到一份数据副本
多播:消息发送给一个多播地址,网络只将数据分发给那些表示想要接收到该多播地址的数据的主机.

广播ip4地址:255.255.255.255
多播ip4地址:224.0.0.0-239.255.255.255
多播例子:

1.加入到指定多播组,准备接收消息

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Arrays;
public class VoteMulticastReceiver {
    public static void main(String[] args) throws IOException {
        InetAddress address = InetAddress.getByName("239.1.1.1"); //指定一个多播地址
        if (!address.isMulticastAddress()) { //检测指定的地址是否为多播地址
            throw new IllegalArgumentException("Not a multicast address");
        }

        MulticastSocket sock = new MulticastSocket(9000); //用于接收的MulticastSocket,并指定多播端口
        sock.joinGroup(address); //加入这个多播组

        VoteMsgTextCoder coder = new VoteMsgTextCoder();
        // Receive a datagram
        DatagramPacket packet = new DatagramPacket(new byte[VoteMsgTextCoder.MAX_WIRE_LENGTH],VoteMsgTextCoder.MAX_WIRE_LENGTH);
        sock.receive(packet);//接收数据
        VoteMsg vote = coder.fromWire(Arrays.copyOfRange(packet.getData(), 0, packet.getLength()));
        System.out.println("Received Text-Encoded Request (" + packet.getLength() + " bytes): ");
        System.out.println(vote);
        sock.close();
    }
}

2.发送消息测试

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class VoteMulticastSender {
    public static final int CANDIDATEID = 475;
    public static void main(String args[]) throws IOException {
        InetAddress destAddr = InetAddress.getByName("239.1.1.1"); //指定目标多播地址
        if (!destAddr.isMulticastAddress()) { //一样检测多播地址是否有效
            throw new IllegalArgumentException("Not a multicast address");
        }

        int destPort = 9000; //目标多播端口
        int TTL = 4; //设置生命周期:路由器转发不能超过的次数(每经一个路由器递减1)

        MulticastSocket sock = new MulticastSocket();
        sock.setTimeToLive(TTL); // Set TTL for all datagrams
        VoteMsgCoder coder = new VoteMsgTextCoder();
        VoteMsg vote = new VoteMsg(true, true, CANDIDATEID, 1000001L);
        // Create and send a datagram
        byte[] msg = coder.toWire(vote);
        DatagramPacket message = new DatagramPacket(msg, msg.length, destAddr, destPort);
        System.out.println("Sending Text-Encoded Request (" + msg.length + " bytes): ");
        System.out.println(vote);
        sock.send(message);
        sock.close();
    }
}

你可能感兴趣的:(Socket多接收者)