Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make two android devices to communicate through TCP

Tags:

We want to establish TCP/IP connection between two android devices. For now we thought that it would be simpler if we make the connection device to device. So there is no server that is between the two phones.

Most of the time (if not always) one has no real IP address (NAT and so on). Is this a problem for creating a TCP socket? I didn't manage to find any exact information for this. Any advice and opinion will be highly appreciated.

Thanks

like image 716
Luben Avatar asked Dec 24 '10 14:12

Luben


1 Answers

Is this a problem for creating a TCP socket?

Answer is no, it does not make it impossible (unless NAT's have an unpredictable way of mapping IP addresses). However, it is not simple to accomplish.

The short answer is you'll always need a server-like peer with a public IP address to facilitate initial communication between peers located behind NATs. The peers connect to it. A tells server it wants to TCP connect with B. Server notifies B. NAT behavior analysis is performed and if possible, A and B attempt to open TCP communications with each other by predicting the next mapping of the other NAT. If all goes fine (and usually it does) A touches base with B or vice-versa. They close the other connection attempts and communicate with the operational TCP connection. This technique is called STUN for TCP.

Now, there are more complicated reason why things can go wrong. If you want to know more, I have written a book called Practical JXTA II which available for reading online at Scribd. There is a chapter dedicated to NAT traversal.

Hope this helps.

like image 149
Jérôme Verstrynge Avatar answered Nov 14 '22 11:11

Jérôme Verstrynge