简体   繁体   English

用 Java 中的 UDP 广播

[英]Broadcasting with UDP in Java

I have been struggling to understand broadcasting with UDP and haven't been able to implement it in my program.我一直在努力理解使用 UDP 进行广播并且无法在我的程序中实现它。

Currently, I have a Client & Server which both run 2 Threads (one for receiving, one for sending).目前,我有一个客户端和服务器,它们都运行 2 个线程(一个用于接收,一个用于发送)。

The Client can send SET/GET queries to a Server it is connected to, to store/get Key-Value pairs.客户端可以向它连接的服务器发送 SET/GET 查询,以存储/获取键值对。 The Idea is that, with multiple Servers, the Key we are looking for may be on another Server.这个想法是,对于多个服务器,我们正在寻找的密钥可能在另一个服务器上。 Therefore the GET query should be broadcast to all other servers.因此 GET 查询应该广播到所有其他服务器。

private void send(String value, int port) throws IOException {
    System.out.println("Send: " + value);
    byte[] buffer = (value).getBytes();
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, connectingPort);
    socket.send(packet);
}

This is currently the send function of the Sending Thread for the Client.目前这是客户端的发送线程的发送 function。

As InetAddress I use localhost.作为 InetAddress,我使用 localhost。

The connectingPort = the Port of the Server that this client is connected to连接端口 = 此客户端连接到的服务器端口

public ClientSendingThread() throws UnknownHostException, SocketException {
    address = InetAddress.getByName("localhost");
    stdin = new BufferedReader(new InputStreamReader(System.in));
    //Registering yourself upon creation
    try {
        socket = new DatagramSocket();
        socket.setBroadcast(true);
        receiver = new ClientReceivingThread(socket);
        System.out.println("Enter port of the Server you would like to connect to: ");
        System.out.println("localhost is used automatically as address");
        connectingPort = Integer.parseInt(stdin.readLine());
        Thread receivingThread = new Thread(receiver);
        receivingThread.start();
        send("Client", connectingPort);
    } catch (IOException ex) {
        System.out.println(ex);
    }
}

That's the constructor of the ClientSendingThread, were I simply ask for User Input for the Port Number to which they want to connect to.那是 ClientSendingThread 的构造函数,我只是要求用户输入他们想要连接的端口号。 Then I start the receiving Thread and send "Client" to the Server, such that it will "register" the Client.然后我启动接收线程并将“客户端”发送到服务器,这样它将“注册”客户端。

My questions about broadcasting:我关于广播的问题:

What Address do I even use?我什至使用什么地址? Which port?哪个港口?

What you want is technically called 'multicast', not 'broadcast'.您想要的在技术上称为“多播”,而不是“广播”。 It allows a server or servers to register a multicast address, to which clients can send messages.它允许一个或多个服务器注册一个多播地址,客户端可以向该地址发送消息。

Both client and server create a MulticastSocket .客户端和服务器都创建一个MulticastSocket You'll need to pick a port number and a multicast address in the 224.xxx space;您需要在 224.xxx 空间中选择一个端口号和一个多播地址 224.0.0.1 is likely suitable, though it's been a while since I did this so I may be a bit rusty. 224.0.0.1 可能是合适的,虽然我已经有一段时间没有这样做了,所以我可能有点生疏了。 You may also pick an 'unused' 224 address for your specific use.您还可以为您的特定用途选择一个“未使用”的 224 地址。

The difference between multicast and broadcast is that broadcast is 'everyone', including random routers, webcams, etc., that really do not care about your application.多播和广播之间的区别在于广播是“所有人”,包括随机路由器、网络摄像头等,它们并不关心您的应用程序。 Broadcast access tends to be a privileged operation.广播访问往往是一种特权操作。 The broadcast address is all-ones in your network.广播地址在您的网络中是全一。 So, if your network is 192.168.1.0/24, then the broadcast address is 192.168.1.255.所以,如果你的网络是 192.168.1.0/24,那么广播地址就是 192.168.1.255。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM