如何编写一个 Android SocketServer 来监听 wifi [英] How to write an Android SocketServer to listen on wifi
问题描述
我已经使用 java.net.SocketServer
编写了一个线程来侦听特定端口.它在 android 模拟器中运行良好(使用端口转发).我打算在使用应用程序时通过 wifi 连接到这个端口.但是,SocketServer
文档说,如果您不提供 InetAddress
,服务器会在 localhost 上侦听.
I've written a thread using java.net.SocketServer
to listen on a particular port. It works fine in the android simulator (using port forwarding). I'm planning to connect over wifi to this port when the app is being used. However, the SocketServer
documentation says that if you don't supply an InetAddress
, the server listens on localhost.
如果我不提供地址,我将无法通过 wifi 连接,我是否正确?如何将 wifi 连接的 InetAddress
传递给 SocketServer
?
Am I correct that if I do not supply the address, I will not be able to get a connection over wifi? How can I get the InetAddress
of the wifi connection to pass to the SocketServer
?
推荐答案
当您创建 ServerSocket 时,您会监听 localhost 上的 端口.如果你想提名你自己的 本地主机地址.
When you create a ServerSocket you listen to a port on the localhost. It's up to you if you want to nominate your own local host address.
阅读这两篇文章:
记得有一个 WiFi 锁和适当的权限.
Remember to have a WiFi lock and the appropriate permissions.
这篇关于如何编写一个 Android SocketServer 来监听 wifi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!