简体   繁体   中英

ServerSocket is using ip /::

I'm making a chat application for android and currently testing with 2 emulators and a phone connected via usb. I have not been able to get a Socket to connect to a ServerSocket for pretty much the last 24 hours. Each host is using a ServerSocket in order to listen to other hosts "talk" via their client Sockets.

For both emulators, the following 2 lines of code print out: The server socket is using ip: /::

serverSocket = new ServerSocket(portNo);
Log.i("MY_TAG", "The server socket is using ip: " + serverSocket.getInetAddress() );

Using the following line of code, I get a ConnectionException saying it can't connect to the specified ip and port and that the connection timed out:

socket = new Socket(addr, portNo);

The way I'm obtaining the ip is by querying a server about every 20 seconds, and in a php script, I use $_SERVER['REMOTE_ADDR'] which is indeed returning my ip address.

When using the phone to send a message to an emulator, I get a slightly different error. I get a ConnectionException just like with the emulators, except it also says "Connection refused".

When sending a message from an emulator to the phone, I get a ConnectionException due to timing out.

The phone's ServerSocket is using ip 0.0.0.0 which I know is fine because that means it's just listening on all interfaces.

In all cases, the Server Socket's accept() method never returns.

Using netstat -an (I'm using Windows as my OS), I did not see any of the ports that my ServerSockets were supposedly listening on listed in the output.

Using telnet, I get a connection refused message when attempted on the emulators (using php's $_SERVER['REMOTE_ADDR'] to get the ip).
And I get a connection timeout when using telnet on the phone's ip and listening port.

That said, please help me figure out how to get these sockets connected.

The simple answer is what I was trying to do isn't possible (at least not without jumping through some hoops). The problem is both hosts are behind NAT routers. The way NAT routers work is when an incoming message gets to them, they look at the port number in the message and then translate it to an internal ip and port number corresponding to some host sitting in the local network hiding behind it. This is done using something called the NAT translation table. The problem with my design is that forincoming messages from the world to the NAT, I'm feeding the NAT the internal port number when I should really be feeding it whatever port number the NAT assigned to the host's internal port when a message was sent out. Here's a picture to describe what I'm saying (taken from lecture slides by Alex C. Snoeren, an awesome networking professor at UCSD): 在此输入图像描述

My solution will be to redesign what I'm doing by having a single ServerSocket listen all the time for new connections on some webhost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM