Java – Source of SocketTimeoutExceptions

Source of SocketTimeoutExceptions… here is a solution to the problem.

Source of SocketTimeoutExceptions

Sorry if this has been posted in a few days where I work/look for.

Question:

When communicating with an Android device over a peer-to-peer network, I receive SocketTimeoutExceptions.

Details:

When this happens, the Android device uses its own mobile network instead of WiFi (WiFi wasn’t designed for this in the first place).

Android devices successfully obtain their ipAddress from the whatismyip.com.

One device will send its IP address to another device through a means other than the peer-to-peer network we are using.

The IP address of the remote device is successfully transmitted to the local computer.

A

TimeoutException occurs when the local device responds over a peer-to-peer network.

I tried :

Extend the timeout to a considerable amount of time.

Use a different Android device.

Noteworthy:

It should be noted that this same peer-to-peer network has been tested to work successfully between PCs (running Linux and Windows, if important) on a local area network. Also just tested the peer-to-peer network with a wired internet connection on our PC and it really works.

My guess:

I

think the device is somehow hidden by the ISP and I’m going to have to write some code to do something like NAT traversal, but I’m not too sure. But then again, I should get a connection denied exception if the device is hidden.

Any help is greatly appreciated.

P.S. I can’t post code at the moment, and even if I could, the whole process would have thousands of lines of code.

Solution

First, my peer-to-peer experience was limited to wifi connections, but I believe some concepts apply to 3G as well.

When you receive a connection denied error, it means that your network packets have been able to reach another device, but that device refuses to accept the connection request, and a negative reply reaches your device.

When you encounter a connection timeout error, it means that your network packets could not reach other devices, or that answers from other devices did not reach your device.

Now let’s focus on the second error (connection timeout):

The first step in debugging it is to start the device waiting to connect, and then issue the command from a Windows or Linux computer with network access:

telnet deviceIp portWaitingForConnection

If you see a telnet screen and there are no timeout errors, the problem is in the code that sent the request connection.

If you also get a timeout error, confirm that your network packet cannot reach the device (or the answer cannot reach you).

Now, it might be something that is preventing network packets from reaching the device. The device may be behind a router or firewall.

If the device is behind a firewall, you need to open firewall ports for incoming connections.

If the device is

behind a router (which I believe is the case, otherwise you don’t need to get an IP address from the whatismyip.com), you need to configure the router to allow redirection of incoming requests on the listening port to the IP address of the local device.

I can’t help much without the code.

Good luck.

Related Problems and Solutions