问题描述
我试图用的ServerSocket
在Android设备上的端口上侦听。我希望能够使用计算机在同一网络上连接到这个端口通过WiFi。
我当它绑定到一个端口得到也不例外,但是当我检查netstat的,它说:
原的recv-Q发送-Q本地地址外国地址状态
TCP:0(零):4040(空):* LISTEN
我试过结合到本地主机,0.0.0.0无数的方法,装置与 SocketInetAddress
和 InetAddress类的无线局域网的IP地址.getByName
。似乎没有任何工作。
当我尝试从在同一无线网络的计算机连接到端口(我都试过的netcat和Java的 Socket.connect()的
),所有我能看到Wireshark是一个ARP请求:
谁有[手机的LAN地址]?告诉[计算机局域网的地址。
此请求重演,直到超时。
我已经试过了反向的方式,通过设置ServerSocket的计算机上,并连接到该端口的电话,说工作得很好。
我测试的手机是三星I5700角宿一与定制ROM。
任何想法?
编辑:在code是简单:
的ServerSocket服务器=新的ServerSocket();
server.setReuseAddr(真正的);
server.setTimeout(0);
server.bind(新InetSocketAddress(4040));
客户端的Socket = NULL;
而((客户端= server.accept())== NULL);
//连接
在这里输入code
在这里输入code
而不是使用server.bind,尽量初始化像这样的服务器套接字:服务器=新的ServerSocket(4040);
此外,server.accept()实际上将被阻塞,直到建立连接,这样你就不会需要一个while循环(见的() )
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
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上使用的ServerSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!