0

I have a problem with my code; I am programming an application that sends a small videos to a server. I use the UDP/IP protocol. The problem is the following: When I start the server and the Client on my PC and use the loopback address 127.0.0.1 as server address, it works fine, if the communications is between two computers on the same LAN, so I use the private ip address it also works fine. But if I want to make the comunicate over internet and I use my public ip, which is Fastweb, the server does not receive anything. I have already port forwarded my router with the right port. Here the code of the server:


        # Initializing server socket
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.BUFF_SIZE)
        socket_address = (self.host_ip, self.port)
        server_socket.bind(socket_address)
        print('Listening at:', socket_address)

        count = 0
        sec = 0

        while True:
            # Waiting for connection
            msg, client_addr = server_socket.recvfrom(self.BUFF_SIZE)
            print('GOT connection from ', client_addr)
            # Communicating info video name
            message = base64.b64encode(bytes(self.id_video, 'UTF-8'))
            server_socket.sendto(message, client_addr)
            while True:
                self.vidcap.set(cv2.CAP_PROP_POS_MSEC, sec * 1000)
                hasFrames, frame = self.vidcap.read()
                if hasFrames:

                    # Encoding
                    delta_i = time.time_ns()
                    en = enc.encoder(frame)
                    en.getFrame(count)

                    # Sending
                    frame = cv2.imread(paths_directory.path_dir_ts + "/" + (str(count) + ".jpg"))
                    encoded, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
                    message = base64.b64encode(buffer)
                    server_socket.sendto(message, client_addr)

                    # Adjusting the rate
                    count += 1
                    delta_f = time.time_ns()
                    delta = delta_f - delta_i
                    wait = 1
                    if delta < self.TS:
                        wait = self.TS - delta

                    sec = round((sec + (1 / self.FPS)), 2)

                    # Showing video sent
                    cv2.imshow('TRANSMITTING VIDEO', frame)
                    # Waiting if transmission is too fast
                    cv2.waitKey(int(wait))

                # Sending stop message
                if keyboard.is_pressed("e"):
                    base64_bytes = base64.b64encode(paths_directory.stop_msg)
                    server_socket.sendto(base64_bytes, client_addr)
                    cv2.destroyWindow("TRANSMITTING VIDEO")
                    server_socket.close()
                    break
            while True:
                self.vidcap.set(cv2.CAP_PROP_POS_MSEC, sec * 1000)
                hasFrames, frame = self.vidcap.read()
                if hasFrames:

                    # Encoding
                    delta_i = time.time_ns()
                    en = enc.encoder(frame)
                    en.getFrame(count)

                    # Sending
                    frame = cv2.imread(paths_directory.path_dir_ts + "/" + (str(count) + ".jpg"))
                    encoded, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
                    message = base64.b64encode(buffer)
                    server_socket.sendto(message, client_addr)

                    # Adjusting the rate
                    count += 1
                    delta_f = time.time_ns()
                    delta = delta_f - delta_i
                    wait = 1
                    if delta < self.TS:
                        wait = self.TS - delta

                    sec = round((sec + (1 / self.FPS)), 2)

                    # Showing video sent
                    cv2.imshow('TRANSMITTING VIDEO', frame)
                    # Waiting if transmission is too fast
                    cv2.waitKey(int(wait))

                # Sending stop message
                if keyboard.is_pressed("e"):
                    base64_bytes = base64.b64encode(paths_directory.stop_msg)
                    server_socket.sendto(base64_bytes, client_addr)
                    cv2.destroyWindow("TRANSMITTING VIDEO")
                    server_socket.close()
                    break            while True:
                self.vidcap.set(cv2.CAP_PROP_POS_MSEC, sec * 1000)
                hasFrames, frame = self.vidcap.read()
                if hasFrames:

                    # Encoding
                    delta_i = time.time_ns()
                    en = enc.encoder(frame)
                    en.getFrame(count)

                    # Sending
                    frame = cv2.imread(paths_directory.path_dir_ts + "/" + (str(count) + ".jpg"))
                    encoded, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
                    message = base64.b64encode(buffer)
                    server_socket.sendto(message, client_addr)

                    # Adjusting the rate
                    count += 1
                    delta_f = time.time_ns()
                    delta = delta_f - delta_i
                    wait = 1
                    if delta < self.TS:
                        wait = self.TS - delta

                    sec = round((sec + (1 / self.FPS)), 2)

                    # Showing video sent
                    cv2.imshow('TRANSMITTING VIDEO', frame)
                    # Waiting if transmission is too fast
                    cv2.waitKey(int(wait))

                # Sending stop message
                if keyboard.is_pressed("e"):
                    base64_bytes = base64.b64encode(paths_directory.stop_msg)
                    server_socket.sendto(base64_bytes, client_addr)
                    cv2.destroyWindow("TRANSMITTING VIDEO")
                    server_socket.close()
                    break

Here the client code:

# Initializing client socket
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.BUFF_SIZE)
        client_socket.sendto(message, (self.host_ip, self.port))

        # Used for knowing which video is been transmitted
        packet, _ = client_socket.recvfrom(self.BUFF_SIZE)
        id_video = base64.b64decode(packet, ' /')
        id_video = str(id_video)
        id_video = id_video.replace("b'", "")
        id_video = id_video.replace("'", "")

        frameN = 0
        latency = []
        while True:

            # Receiving
            packet, _ = client_socket.recvfrom(self.BUFF_SIZE)
            # Decoding
            data = base64.b64decode(packet, ' /')
            # ecc.. 
dissy
  • 1
  • 1
    Are you sure you forwarded the UDP port, not the TCP port with the same number? Also check the firewall. As for the code itself, you didn't show the IP address. Remember to use the LAN IP (or "0.0.0.0"), not the public one, since your machine is behind a NAT. IPv4 vs. a public-facing IPv6 may also be an issue. – Homer512 May 11 '22 at 09:33
  • Also check how big the packets are and try sending smaller ones, see if they work. There may be a network-dependent maximum size. – user253751 May 11 '22 at 10:05
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 11 '22 at 11:02
  • Yes I correctly forwarded the UDP port , I called the Fastweb customer service; also I don’t understand I use the private IP address of the server for binding but the public for the client request , is it right? Plus I disabled all the firewall and checked the maximum size. Is there something wrong with the code? – dissy May 11 '22 at 21:54

0 Answers0