如何在java网络中广播
这是我第一次用java进行网络编程。 我正在寻找一种以某种方式向整个网络中的所有节点发送广播的方法。让他们知道我的存在。 我正在尝试制作一款多人网络游戏,我希望客户能够看到所有可用的游戏以选择加入哪一个。 我想知道如何从服务器广播以及如何让客户端收听。
请简单一点,我是新手:)
提前致谢。
Its my first time programing network in java.
I was looking for a way to send to somehow broadcast to all nodes in the whole networking. To let them know of my existence.
I'm trying to make a multiplayer network game, and I want the clients to be able to see all the games available to choose which one to join.
I want to know how to broadcast from the server and also how to make the clients listen.
Please make it simple, I'm a newbie :)
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要广播数据包,请将它们发送到给定子网的广播地址(子网的最后一个地址)。 IP
255.255.255.255
是零网络的广播地址。广播地址
因此,要广播到您当前的网络,请将数据包发送到
255.255.255.255< /代码>。
To broadcast data packets, send them to the broadcast address of the given subnet (the last address of the subnet). The IP
255.255.255.255
is the broadcast address for the zero network.Broadcast address
So to broadcast to your current network, send the packets to
255.255.255.255
.不要混淆术语。
广播通常用于UDP。 UDP 不可靠,因为它不检查客户端是否收到所有数据包。向大量客户端打开大量 TCP 连接不会被广播。
要让您的客户端侦听端口,您需要使用 ServerSocket 并读取它。
Do not confuse terms.
Broadcast is usually used for UDP. UDP is unreliable in the sense that it does not check if all of the packets are received by the clients. Opening a lot of TCP connections to a lot of clients is not broadcast.
To have your clients listen to a port, you need to use ServerSocket and read it.
我建议您使用 PubSubHubbub 或类似协议。基本上,您将有一个“中心”,您可以将要“广播”的通知发送到该“中心”。每个节点都通过提供一个 URL 来订阅该主题,当新数据到达时,集线器可以调用该 URL。当“集线器”收到此广播时,集线器会联系每个订阅 URL,让节点知道有新数据。
I recommend that you use PubSubHubbub or a similar protocol. Basically, you would have a "hub" to which you send the notification that you want to have "broadcasted". Each of the nodes subscribes to the topic, by providing a URL that the hub can invoke when new data has arrived. When the "hub" receives this broadcast, the hub contacts each subscription URL to let the node know there is new data.