Java 中使用多播的网络发现

发布于 2024-09-10 01:49:29 字数 214 浏览 4 评论 0 原文

我正在尝试制作一个客户端/服务器 Java 应用程序。客户端和服务器都将在同一 Wi-Fi 网络上运行。服务器将在客户端知道的特定端口上运行。

我计划通过网络从客户端向该特定端口发送多播消息以发现服务器。但是,我不太确定如何找出网络中的哪个 IP 收到了我的消息。

我是否需要在客户端上创建一个套接字,并在发送多播消息后侦听传入的数据包,以防服务器回复?

提前致谢。

I'm trying to make a client/server Java App. Both client and server will be running on the same wi-fi network. Server will be running on a specific port that client is aware of.

I am planning to send a multicast message from client through the network for that specific port to discover the server. However, I'm not too sure how I can find out which IP in my network received my message.

Do I need to create a socket on the client and listen to incoming packets once I send my multicast message in case server replies back?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

糖粟与秋泊 2024-09-17 01:49:29

(1)服务器监听预先安排的端口

DatagramSocket s = new DatagramSocket(8888);
s.receive  //(1)
s.send     //(2)

(3)客户端向该端口发送消息,在广播 IP 255.255.255.255 上

DatagramSocket c = new DatagramSocket();
c.send(255.255.255.255:8888,msg)     //(3)
c.receive  //(4)

客户端也绑定到端口。我们没有指定它,所以它是我们随机选择的。

(3)将消息广播到所有本地机器,(1)处的服务器接收消息,带有客户端IP:端口。

(2) 服务器向客户端IP:端口发送响应消息

(4) 客户端从服务器获取响应消息。

(1)server listens on a pre-arranged port

DatagramSocket s = new DatagramSocket(8888);
s.receive  //(1)
s.send     //(2)

(3)client sends a message to the port, on the broadcast IP, 255.255.255.255

DatagramSocket c = new DatagramSocket();
c.send(255.255.255.255:8888,msg)     //(3)
c.receive  //(4)

the client binds to a port too. we didn't specify it, so it's random chosen for us.

(3) will broadcast the message to all local machines, server at (1) receives message, with the client IP:port.

(2) server sends response message to client IP:port

(4) client gets the reponse message from server.

乙白 2024-09-17 01:49:29

我强烈建议使用 JGroups。它有很多功能并且可以完成所有 UDP 的工作。 JBoss 将其用于集群。

I would strongly recommend using JGroups. It has a lot of features and it will do all the UDP stuff. JBoss uses it for their clustering.

凑诗 2024-09-17 01:49:29

您可以尝试使用 java.net.MulticastSocket (自 Java 1.1 起可用)。如果您不需要库的丰富功能集,例如 jgroupshazelcast 等简单的 Java API 可能足以为您服务。

另请参阅此处这里

You can try using java.net.MulticastSocket (available since Java 1.1). If you don't need the rich feature sets of libs like jgroups, hazelcast etc. that plain Java API might serve you well enough.

See also example pages here and here.

吻安 2024-09-17 01:49:29

您可以尝试使用 SSDP。 UPnP 设备使用它来发现彼此。它在端口 1900 上进行多播,仅使用非常简单的数据包来发送 IP 和服务信息。

Cling 是一个 UPnP 库,您可以从中获取。注意我不建议您迁移到 UPnP - 只是使用发现协议。

You could try using SSDP. It's what UPnP devices use to discover each other. It's multicast on port 1900 and just uses really simple packets to send around IPs and service information.

Cling is a UPnP lib you can pull from. Note I'm not recommending you move to UPnP - just the discovery protocol used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文