0

I'm trying to do some P2P using UDP. I was able to get my external IP and port, however, when I trigger the other script to use that IP and Port to forward some data, It never reaches the client where I performed the STUN request.

I read about not closing the socket because this could cause the NAT to reallocate that port, but even if I let the socket opened, the socket never receives the data that I try to send from the other client.

I'm using pystun to perform the STUN request.

Here are my snippets:

Client A (The one querying the external IP and port)

import socket
from stun import get_nat_type

# Creating the socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(20)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("0.0.0.0",15000))

# Querying a STUN server to get IP address and port
nat_type, nat = get_nat_type(s, "0.0.0.0", 15000,
                                stun_host=None, stun_port=3478)
external_ip = nat['ExternalIP']
external_port = nat['ExternalPort']

# Displaying STUN data and listening for client B  to send data
print("Punch hole: ('{0}',{1})".format(external_ip, external_port))
print("listening port for incoming data")
print(s.recvfrom(4096)[0])

Client B (The one sending data via the external IP and port

from socket import *
s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s.sendto(b"hey!", ('<External IP of Client A>', 11224))

The "Hey" sent by client B is never reaching client A

The pystun that I'm using is that one https://github.com/talkiq/pystun3

Felipe
  • 21
  • 1
  • 3
  • 1
    The other side needs to do STUN as well. Then they exchange addresses. Then they do a hole punching step involving multiple retries. Finally they connect. Everything you need to know is here: https://stackoverflow.com/a/8524609/104458 – selbie Jan 18 '23 at 07:30
  • Ohh yeah, Thanks, It works, I just had to do the STUN on both sides, Thanks @selbie – Felipe Jan 18 '23 at 15:53

0 Answers0