发现 WiFi 网络上的客户端
我正在编写一个java应用程序,我需要快速发现任何有线或无线本地网络上正在运行的任何其他客户端,以便建立TCP连接。
这样做的最好方法是什么? 是否有库或代码片段可以做到这一点?
I'm writting a java application, and I need to quickly discover any other running clients on any wired or wireless local networks in order to establish a TCP connection.
what's the best way of doing this? Are there libraries or code snippets that would do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个用于可靠多播通信的 JGroups 工具包。 它允许使用其他答案中所述的多播技术自动发现其他客户端。
它还提供基于多播套接字的通信 API。
它被用于 JBoss、Tomcat 等许多项目中,为分布式缓存提供基础设施。 请参阅此处了解更多信息。
There is a JGroups toolkit for reliable multicast communications. It allows automatic discovery of additional clients using Multicast techniques as described in other answers.
It also provides communication APIs on top of multicast sockets.
It is used in a number of projects such as JBoss, Tomcat and more to provide an infrastructure for distributed cache. See more here.
我想您需要对子网中所有 IP 上的应用程序端口进行扫描。
可用的 IP 是多少 - 或者您的子网是什么?
恐怕确定这一点是不可能的,因为网络被设计为对您的应用程序透明。
所以,我会使用暴力:选择你的IP并更改最后一个字节。 可能太多了,也可能还不够。
或者您发送广播(通常针对 xxx255)并查看谁回答。
请参阅数据报广播和多播。 但我认为这不再是 TCP/IP 了。
I guess you need to do a scan on your application's port on all IPs in your subnet.
Just what are the available IPs - or what is your subnet for that matter?
I'm afraid determining that could turn out to be impossible as the network is designed to be transparent to your application.
So, i'd use brute force: pick your IP and change the last byte. Might be too much, might be not enough though.
Or you send a broadcast (which usually would be targeted at x.x.x.255) and see who answers.
See Datagram Broadcasting and Multicasts. But i think that's not TCP/IP anymore.
网络扫描可能会很长,在无线网络上甚至更长。 如果您通过 Java 快速需要它们,您可以在网络上实现“会面点”服务器。 该服务器侦听预定义的端口,客户端在启动时在服务器上注册,并且服务器可以根据请求分发有关客户端的信息。
HTH。
A network scan can be very long, even longer on wireless networks. If you need them quickly thru Java you may implement a "meeting point" server on your network. This server listen to a predefined port, clients register on the server on startup and the server can distribute information about the clients on request.
HTH.
多播 UDP 是实现此目的的好方法。 它用于支持通过本地 IP 网络(UPnP 和 ZeroConf)自动发现联网设备的多种技术。
组播 UDP 不是 TCP,但它仍然基于 IP,因此使用相同的寻址机制,即 IP 地址。 通常将其与无线电广播进行比较,即多播发送者仅需要发送 1 条消息(即它就像广播),但只有“调谐”到多播频道的客户端才会收到它。
作为入门,您可以在 google 或 wikipedia 上快速搜索这些内容,但基本思想如下:
如果您正在寻找要使用的库,UPnP 库往往有点重量级,而且很多人通常不喜欢它们,所以 ZeroConf可能更合适一点。 我不知道这些东西的任何 java 实现,但我相信你可以通过一些挖掘找到一些。
Multicast UDP is a good way of doing this. It's used in a couple of technologies that support automatic discovery of networked devices over local IP networks (UPnP and ZeroConf).
Multicast UDP is not TCP, but it is still based on IP and, so, uses the same addressing mechanism i.e. IP addresses. Quite often it is compared to radio broadcasting i.e. a multicast sender only needs to send 1 message (i.e. it is like a broadcast) but only clients that are "tuned-in" to the multicast channel will receive it.
You can do a quick search on google or wikipedia for these as a starter, but the basic idea is as follows:
If you are looking for libraries to use, UPnP libraries can tend to be a bit heavyweight and a lot of folk generally don't like them, so ZeroConf might be a little more suitable. I don't know of any java implementations of such things but I'm sure you can find some with a little digging around.