无法在Android上使用ServerSocket
我正在尝试在 Android 设备上使用 ServerSocket
监听端口。我希望能够使用同一网络上的计算机通过 WiFi 连接到此端口。
将其绑定到端口时,我没有遇到任何异常,但是当我检查 netstat 时,它说:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 (null):4040 (null):* LISTEN
我已经尝试了无数种方法将其绑定到 localhost、0.0.0.0、设备的 WiFi LAN IP 地址和 SocketInetAddress代码>和<代码>InetAddress.getByName。似乎什么都不起作用。
当我尝试从同一 WiFi 中的计算机连接到端口时(我尝试了 netcat 和 Java 的 Socket.connect()),我在 Wireshark 中只能看到一个 ARP 请求
Who has [phone's LAN address]? Tell [computer LAN address].
:请求重复自身直到超时。
我尝试了相反的方法,通过在计算机上设置 ServerSocket 并从手机连接到该端口,效果非常好。
我的测试手机是带有自定义 ROM 的 Samsung Spica i5700。
有什么想法吗?
编辑: 代码很简单,如下所示:
ServerSocket server = new ServerSocket();
server.setReuseAddr(true);
server.setTimeout(0);
server.bind(new InetSocketAddress(4040));
Socket client = null;
while((client = server.accept()) == null);
// Connected
enter code here
enter code here
I'm trying to listen on a port using ServerSocket
on an Android device. I want to be able to connect to this port over WiFi using a computer on the same network.
I get no exception when binding it to a port, however when I check netstat it says:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 (null):4040 (null):* LISTEN
I've tried countless ways of binding it to localhost, 0.0.0.0, the WiFi LAN IP address of the device with SocketInetAddress
and InetAddress.getByName
. Nothing seems to work.
When I try to connect to the port from a computer in the same WiFi (I've tried both netcat and Java's Socket.connect()
), all I can see in Wireshark is an ARP request:
Who has [phone's LAN address]? Tell [computer LAN address].
This request repeat itself until timed out.
I've tried the reverse way, by setting the ServerSocket on the computer and connecting to that port from the phone, that works very well.
My testing phone is an Samsung Spica i5700 with a custom ROM.
Any ideas?
Edit:
The code is simple as this:
ServerSocket server = new ServerSocket();
server.setReuseAddr(true);
server.setTimeout(0);
server.bind(new InetSocketAddress(4040));
Socket client = null;
while((client = server.accept()) == null);
// Connected
enter code here
enter code here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用 server.bind,而是尝试像这样初始化服务器套接字:
服务器 = 新的 ServerSocket(4040);
另外, server.accept() 实际上会阻塞,直到建立连接,因此您不需要 while 循环(请参阅:http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html#accept())
Instead of using server.bind, try initializing the server socket like this:
server = new ServerSocket(4040);
Also, server.accept() will actually block until a connection is made, so you don't need that while loop (see: http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html#accept() )
我也遇到了这个问题,只能通过以下方式连接到我的Android服务器:
其中主机名是本地IP,我使用此页面中的 getLocalIpAddress() 函数获得:
https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye /MainActivity.java
I struggled with this too and was only able to connect to my Android server by using:
Where hostname was the local IP, which I got using the getLocalIpAddress() function from this page:
https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye/MainActivity.java
我可以通过使用
手机的 where addr = InetAddress 来实现此功能。否则,它似乎只绑定到本地主机(127.0.0.1)。另外,我使用的是 8080 端口。
I was able to get this working by using
where addr = InetAddress of your phone. Otherwise, it only seems to bind to localhost (127.0.0.1). Also, I'm using port 8080.