Lightyear Buzz
Lightyear Buzz

Reputation: 796

Source of SocketTimeoutExceptions

Sorry if this has been posted somewhere I've been working/looking for days.

The Problem:

I'm getting SocketTimeoutExceptions when communicating via a peer to peer network with android devices.

Details:

The android devices use their own mobile network and not wifi when this happens(wifi wasn't designed to work with this anyways).

The android devices successfully get their ipAddress from whatismyip.com.

One device will send the other their ip address via other means than the peer to peer network we're using.

The ip address of the remote device will come successfully to the local device.

When the local device responds via the peer to peer network a TimeoutException occurs.

What I've tried:

Extending the timeout to significantly long periods of time.

Using other android devices.

Notable:

It should be noted that this same peer to peer network has been tested to successfully work amongst PCs(running linux and windows if that matters) on a local area network. Also just tested the peer to peer network across our wired internet connections on our PCs and it definitely works.

My guess:

I'm thinking that the device is hidden by the isp somehow and I'm going to have to write some code to do something similar to nat traversal but I'm not quite sure. But then again if the device is being hidden I should get a connection refused exception.

Any help is much appreciated.

P.S. I'm unable to post code at the moment and even if I could it's a couple thousand lines of code for the whole process.

Upvotes: 3

Views: 367

Answers (3)

sokie
sokie

Reputation: 1986

What you should try is to effectively debug your connection,the first thing you could do is check isReachable as hinted in android dev even if unfortunately some networks blocks ICMP.

Another important thing to note is even if you use 3g,your network can still use NAT addresses,and it is very common believe me,so the ip you see on whatsmyip could aswell be a NATed ip.

Next step i would reccomend is to try to use your app in a hybrid mode,that is one pc and one android device and try to connect to the pc(make sure that one has public ip or atleast forwarded ports) if that works you know for sure it is your mobile carrier.

If that really proves to be the case,there is nothing you can do except use wifi,or use a local server to establish connection between two peers.

Also to be noted that p2p networks get stronger the more they grow,you could aswell implement that peers already connected to the network and reachable from the outside help those others to connect to the other peers.You should look up p2p relaying and Hole punching

Upvotes: 1

Tobias Ritzau
Tobias Ritzau

Reputation: 3327

It sounds as you are having nat issues. Here are some start points to get around it (but I think that you have to switch to udp):

You could of course also introduce a server. Happy hacking :-)

Upvotes: 2

Luis
Luis

Reputation: 12048

First of all, my experience with peer to peer is only with wifi connections, but I believe some concepts should also apply to 3G.

When you get a connection refused error, it means you network packets have been able to reach the other device, but the device refused to accept the connection request, and the negative answer reach your device.

When you get a connection timeout error, it means your network packet wasn't able to reach the other device, or the answer from the other device didn't reach yours.

Now let's focous on the second error (connection timeout):

First step to debug it, is starting the the device to be waiting for the connection, and then from a windows or linux machine with network access, issue the command:

telnet deviceIp portWaitingForConnection

If you get a telnet screen and no timeout error, means the issue is in your code that send the request connection.

If you also get the timeout error, it's confirmed that your network packet can't reach the device (or the answer can't reach you)

Now, whats could be preventing network packets from reaching the device. The device may be behind a router or a firewall.

If a device is behind a firewall, you need to open the firewall port for incomming connections.

If the device is behind a router (which I believe is the case, otherwisw you wouldn't need to get the ip address from whatismyip.com) you need to configure the router to let incoming requests on your listening port be redirected to the local device ip address.

I can't help much more without the relevant code.

good luck.

Upvotes: 2

Related Questions