'How to make a socket communicate between two LAN?

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.. 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source