发现 WiFi 网络上的客户端

发布于 2024-07-28 09:55:13 字数 108 浏览 5 评论 0原文

我正在编写一个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 技术交流群。

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

发布评论

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

评论(4

烟花肆意 2024-08-04 09:58:20

有一个用于可靠多播通信的 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.

温柔嚣张 2024-08-04 09:57:43

我想您需要对子网中所有 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.

聆听风音 2024-08-04 09:57:10

网络扫描可能会很长,在无线网络上甚至更长。 如果您通过 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.

最冷一天 2024-08-04 09:56:37

多播 UDP 是实现此目的的好方法。 它用于支持通过本地 IP 网络(UPnP 和 ZeroConf)自动发现联网设备的多种技术。

组播 UDP 不是 TCP,但它仍然基于 IP,因此使用相同的寻址机制,即 IP 地址。 通常将其与无线电广播进行比较,即多播发送者仅需要发送 1 条消息(即它就像广播),但只有“调谐”到多播频道的客户端才会收到它。

作为入门,您可以在 google 或 wikipedia 上快速搜索这些内容,但基本思想如下:

  • 当客户端启动时,它会向某个预先指定的多播地址和端口(例如UPnP 使用 239.255.255.250:1900)
  • 现有客户端正在侦听指定地址和端口上传入的多播“hello”消息 - 当客户端收到消息时,它会向发送者发送响应,
  • 发送“hello”消息的客户端会收到响应来自网络上每个现有客户端,现在知道每个客户端的 IP 地址

如果您正在寻找要使用的库,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:

  • when a client starts, it sends out a multicast UDP "hello" message to some pre-specified multicast address and port (e.g. UPnP uses 239.255.255.250:1900)
  • existing clients are listening for incoming multicast "hello" messages on the specified address and port - when a client receives one, it sends a response to the sender
  • the client sending the "hello" message receives a response from each existing client on the network and is now aware of the IP address of each client

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.

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