A
A
AlexGenesis2022-03-31 12:21:17
Python
AlexGenesis, 2022-03-31 12:21:17

Why doesn't UDP hole punching work?

Good afternoon, there is an attempt at my own implementation of UDP-hp, but not working, I will be grateful for any answer.
Code:
Server:

import socket
from pickle import dumps

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("192.168.0.122", 4545))

msg, addr1 = sock.recvfrom(1024)
msg, addr2 = sock.recvfrom(1024)

sock.sendto(dumps(addr2), addr1)
sock.sendto(dumps(addr1), addr2)

Client (identical code for both clients):
import socket
from pickle import loads

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("192.168.0.122", 5000050001 для второго клиента)))


server = ("Белый ip адрес сервера", 4545)
sock.sendto("Hi".encode(), server)

msg, addr = sock.recvfrom(1024)

another = loads(msg)

sock.settimeout(0.1)

while True:
    try:
        sock.sendto("Hello".encode(), another)
        cl, ms = sock.recvfrom(1024)
        print(cl.decode())
    except socket.timeout:
        continue


Port 4545 has been forwarded on the server.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zolg, 2022-03-31
@Zolg

All three work for you on one machine? It is not very clear - what effect are you trying to achieve then and in which place exactly to break through. draw yourself packet paths, especially through the firewall rules on the router.
Well, yes, not every nut breaks through in such a naive way. If we talk about providers, then rather - almost none

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question