I'am trying to send UDP packets with images to my friend who is behind his NAT. He forwarded his receiving port but the packets don't arrive using our python code. But nevertheless, when using test code the packet does arrive and the tuple containing IPaddr and PORT are the same.

NOTE: not a single exception was triggered, it simply gets stuck in the recvfrom because there is nothing to read

This is the sample code that I used to test if the packets arrive and it worked:

import socket

IPADDR = '88.1.231.55'
PORTNUM = 8043

PACKETDATA = 'TEEEESSTT'.encode('ascii')

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)

s.sendto(PACKETDATA, (IPADDR, PORTNUM))

s.close()

Init of the UDP class:

def __init__ (self, ip_dest, port_dest, port_src, tcp):
        self.TCP = tcp
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.dest = (str(ip_dest), int(port_dest))
        self.socket.bind(('', port_src))

        self.receiveVideoThread = threading.Thread(target=self.receive_video)
        self.receiveVideoThread.setDaemon(True)
        self.receiveVideoThread.start()
        ...........

Send the image:

def send_video(self, frame):
        if not self.TCP.pauseCall or not self.TCP.endCall:
            try:
                header = "{}#{}#{}#{}#".format(self.counter, time.time(), '640x480', self.fps).encode("utf-8")
                ratio = self.get_compress_ratio(self.fps_friend)
                frame = self.rescale_by_height(frame, 320)
                self.frame_sent = frame
                frame_compressed = self.compress(frame, 1)
                self.socket.sendto(header +  frame_compressed, self.dest)
                self.counter += 1
            except Exception as e:
                print(e)
                return

And recieve:

def receive_video(self):
        while not self.TCP.endCall or not self.TCP.pauseCall:
            try:
                data, _ = self.socket.recvfrom(200)
                datagram = data.split(b'#', 4)
                self.fps_friend = datagram[3]
                ........

When we use our code the packets show up on Wireshark but never arrive. When using the sample code the packet does arrive.

Could it be something related to the size of the packets? I've tried compressing the images at the maximum so now the size of an entire UDP packet is ~5KB

0 Answers