简体   繁体   中英

TCP connection between two android devices (even with NAT)

I am doing an android app allowing users to play online.

Currently, I use a TCP server: when two persons are connected, the server takes care of forwarding the packets between the two clients.

I would like to replace my server by a java servlet with google app engine. This new server will just be used to connect the two players.

It would work in that way: Player A opens a server socket and then post to the server the connection details. When a player B wants to play against A, he asks to the server the port number of A and he connects directly to A.

The problem is that I am not sure that it will work if player A is behind a NAT. When player A opens a server socket, that opens one port of its 192.168.xy address, but does it ask to the box a port forwarding? I assume it doesn't...

So two questions: Is it possible to make a direct connection TCP between two devices even when there is a NAT or a firewall (I don't know how firewalls work on Android...) If it isn't possible, what is the best solution: Is it possible to make a TCP server to ensure the exchange of the messages with app engine?

Thank you by advance.

game

First, the device itself is probably not going to be the main problem. If they are at home and using WiFi, you will probably have to deal with a cable modem/DSL modem, which typically includes a firewall. Also if they are at work (or a hotel, conference center, etc.), there may be a corporate firewall to deal with.

I believe most home cable/DSL modems support uPnP (Universal Plug and Play), which includes the Internet Gateway Device Protocol (IGD) designed to let devices determine the external IP address and set up port mappings. In general you can look up NAT traversal for ways to handle connections through a home modem/firewall. I will note that corporate firewalls are a different matter and many of these techniques won't work.

So probably I would recommend you be ready for at least the following four scenarios

  1. Direct connection with nothing creating problems. You can test this by having the server do a test connection when the player first contacts the server. If this works, things are simple.
  2. Home NAT device that understands uPnP. If you have a 10.xxx, 172.16.xx-172.31.xx, or 192.168.xx number (typical home WiFi), then you can try to set up the NAT traversal and if that works you can send the appropriate information to your server. It probably would be worthwhile for the server to do a test connection just to be sure that things work.
  3. If you have a firewall that you can't get around, then make a note on the server regarding player A, and when B tries to join A's game, look and see if B will accept connections, and if so then arrange for A to connect to B instead.
  4. If none of the above work, then have A and B both connect to the server and have the server relay messages between A and B.

If you don't want to program all those possibilities, then option 4 is the one that is most likely to work, even if it does mean extra traffic going to/from your server. But note that for corporate networks, they may simply have a rule blocking unknown connections, and there may not be much you can do.

Edit: I was able to get a simple TCP server working on Android without anything special regarding Android itself, so removed a comment saying I didn't know about that.

Creating direct TCP connection between users under different NAT is mostly possible. There are 4 types of NAT. FC, ARC, PRC, Symmetric. If one of player A or B has symmetric NAT then it is impossible to create TCP P2P connection. In this case you will have to use a server in the middle for exchanging data between two players.

For other types of NAT combinations it is very much possible but not guaranteed. The technique that is used to create TCP P2P connection is called TCP hole punching. Read this answer to know in details about this technique.

Also creating TCP P2P connection is not related to any platform.

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