使同一 LAN 上的两个 Java 应用程序相互了解
我有一个 Java 程序在同一网络上的两台计算机上运行。我希望这些应用程序能够相互了解,这样它们就可以直接通信,而不是与服务器通信来中继消息。
我相信我可能有一个关于如何工作的解决方案,但我无法找到任何示例来比较我的解决方案。大家知道这个问题一般是怎么解决的吗?
I have a Java program running on two computers that are both on the same network. I would like to have these applications become aware of each other, so they could communicate directly as opposed to communicating with the server to relay messages.
I believe i may have a solution as to how this would work, but am unable to find any examples to compare my solution against. Do you guys know how this problem is usually solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个很好的库,可以在 http://jmdns.sourceforge.net/
这基本上减轻了您的协议负担,并允许您根据逻辑名称广告和查找服务提供商(例如 iTunes 或 Mac 打印就是这样做的)。
本书 http://www.amazon.com/Zero-Configuration-Networking-Definitive-Guide /dp/0596101007 解释了所有基本概念。
There is a good library that implements the Zeroconf / Bonjour standard in plain java at http://jmdns.sourceforge.net/
This basically relieves you from the protocol burden and allows you to advertise and lookup service providers based in logical names (That's what iTunes or Mac printing does for example).
This book http://www.amazon.com/Zero-Configuration-Networking-Definitive-Guide/dp/0596101007 explains all basic concepts.
您可以让它们在 LAN 环境中进行 UDP 多播,以使用协议消息来识别程序,然后存储彼此身份的缓存,然后使用 TCP 进行连接并进行主要的消息交换(这比 UDP 更可靠)。或者,您也可以仅在需要时继续使用 UDP 消息传递。
你可以在网上搜索Java中的多播。
一些多播相关链接:
http://download. oracle.com/javase/1.4.2/docs/api/java/net/MulticastSocket.html
http://www.javafaq.nu/java-article817.html
一款不错的多播聊天软件可以参考:
http://sourceforge.net/projects/mc2/
You could get them to do a UDP multicast within a LAN environment to identify the programs using protocol messages then have a stored cache of each other's identity and then use TCP to connect and do main exchanging of messages (which is more reliable than UDP). Or you can simply proceed with UDP messaging only if you want to.
You can search for multicasting in Java online.
Some multicast related links:
http://download.oracle.com/javase/1.4.2/docs/api/java/net/MulticastSocket.html
http://www.javafaq.nu/java-article817.html
A good multicast chat software you can reference:
http://sourceforge.net/projects/mc2/
一种方法是发送广播来查看谁在那里,然后实现一个 GUI 来向用户显示那里有哪些其他对等点,并提供连接选项。 (广播将为您提供那里每个人的 IP 地址。)
一旦您知道要连接到谁,您只需打开 TCP 连接(如果时间紧迫,则使用 UDP)即可完成。
顺便说一句,这是针对 IPv4 的 - IPv6 没有广播(尽管有些类似)。
One way would be to send a broadcast to see who's out there, then implement a GUI to show the user what other peers are there and give an option to connect to. (The broadcast will give you the IP address of everybody there.)
Once you know who to connect to, you simply open a TCP connection (or use UDP if it is time-critical) and you're done.
Btw, this is for IPv4 - IPv6 doesn't have broadcast (although something similar).