简体   繁体   中英

Java TCP Hole Punching

I'm currently developing an online game. Within the game it is necessary to send data from the server to the clients via TCP and UDP. Implementing UDP-Hole Punching is easy, but I'm not really sure how to implement TCP-hole punching:

  1. Server: ServerSocket listening on given port 1
  2. Client: Socket (object) connects to server on port 1 using port 2
  3. Once the connection is established, the server keeps the Socket object from server.accept() and uses it to send data to client for the remaining time
  4. Once the connection is established, the client closes its Socket object and opens a ServerSocket on port 2. The server is now able to send data using a Socket object on port 1 to port 2.

Is 3. or 4. the right way to go?

Option 3.

Option 4 does not work because once the client closes the socket BOTH sides shut down. The server would have to initiate the connection across the NAT firewall, which clearly doesn't work.

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