Android - 在网络中查找服务器
我目前正在编写一个客户端-服务器应用程序,我问自己是否有更好的方法来在本地网络中查找服务器,然后遍历所有可用的 IP 地址并查看是否提供了正确的答案?
I am currently writing a client-server application and I ask myself if there is a better way to find a server in the local network then going trough all the available IP addresses and see if the correct answer is supplied?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要研究 UDP 广播,其中您的服务器会自我宣布,并且手机会侦听广播。
有一个来自 Boxee 远程项目的示例,如下所述。
获取广播地址
您需要访问 wifi 管理器以获取 DHCP 信息并从中构建广播地址:
发送和接收 UDP 广播数据包
构建了广播地址后,一切正常。以下代码将通过广播发送字符串数据,然后等待响应:
您还可以查看 Bonjour/zeroconf,其中有一个 Java 实现,应该可以在 Android 上运行。
You might want to look into UDP broadcasts, where your server announces itself and the phone listens for the broadcasts.
There is an example from the Boxee remote project, quoted below.
Getting the Broadcast Address
You need to access the wifi manager to get the DHCP info and construct a broadcast address from that:
Sending and Receiving UDP Broadcast Packets
Having constructed the broadcast address, things work as normal. The following code would send the string data over broadcast and then wait for a response:
You could also look into Bonjour/zeroconf, and there is a Java implementation that should work on Android.