Leif
Leif

Reputation: 117

UDP Holepunching Issue

So I wrote a simple python server to use for setting up P2P connections (for a game I'm making).

The server code is simple (python):

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ''
server_port = 5000
server = (server_address, server_port)
sock.bind(server)
print("Listening on " + server_address + ":" + str(server_port))
while True:
    payload, client_address = sock.recvfrom(1)  
    resp = client_address[0] + ":" + str(client_address[1])
    print("Echoing data back to " + str(client_address))
    sent = sock.sendto(str.encode(resp), client_address)

It listens for incoming UDP messages and responds with the client's public ip:port info (eg 'xxx.yyy.zzz.aaa:port').

The client receives this info from the server and updates their ip:port info on Firebase. All players in a firebase game lobby can see their opponents ip:port info.

However, the next part - P2P - doesn't work. Once the clients get their opponents public ip:port info and start sending UDP packets, none of them arrive.

Anyone know what the issue might be?

***** EDIT ***** I've solved the issue.. and it was not possible for anyone to answer based on the above info I gave. I was stupidly not renewing the port for receiving new udp packets after getting the first response from the server. Feeling pretty dumb atm. Anyways, the UDP holepunching system seems to be working. My server is the python code above, and the client is a unity game (c#) with a firebase backend. If anyone has questions, please let me know.

Upvotes: 2

Views: 289

Answers (1)

freegnu
freegnu

Reputation: 813

The listening ports would have to be open on the router. You need to go the extra step of adding a UPNP registration.

This is a library with no setup so no pip install https://github.com/jfdelgad/port-forward

This is a library you can pip install https://github.com/flyte/upnpclient

Both have excellent examples of their usage in the README.md

Upvotes: 1

Related Questions