我已经使用 java.SocketServer 编写了一个线程来侦听特定端口.它在 android 模拟器中运行良好(使用端口转发).我打算在使用应用程序时通过 wifi 连接到这个端口.但是,SocketServer 文档说,如果您不提供 InetAddress,服务器会在 localhost 上侦听.
I've written a thread using java.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.
阅读这两篇文章:
- 在Eclipse中实现一个简单的Socket Server
- 使用 java.Socket 的简单通信
记得有一个 WiFi 锁和适当的权限.
Remember to have a WiFi lock and the appropriate permissions.
更多推荐
如何编写一个 Android SocketServer 来监听 wifi
发布评论