0

I'm trying to create an app using python, to watch together movies, but I constantly getting errors with :

[Errno 10040] A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram was smaller than the datagram itself

Server.py

BUFFOR_SIZE_DATA = 65536

# SERVER SETTINGS
server_socket_main = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket_main.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, BUFFOR_SIZE_DATA)

print(Fore.RED + 'UDP Server')
host_name = socket.gethostname()
print('HOST NAME:',host_name)
host_ip_adress = ''
print('HOST IP:',host_ip_adress)
host_port = 1337
socket_adress_main = (host_ip_adress, host_port)
print('PORT:',host_port)
server_socket_main.bind(socket_adress_main)
print(Fore.GREEN + 'Server is listening > > >')
print(Fore.WHITE + ' ')
print(Fore.WHITE + 'Connected devices: ')

# VIDEO U WANT TO WATCH TOGETHER
server_video_main = cv2.VideoCapture('movies/exmpl.mp4')

# MAIN LOOP
while True:
    msg, client_addres_obligatory = server_socket_main.recvfrom(BUFFOR_SIZE_DATA)
    print('Connected from ', client_addres_obligatory)
    WIDTH = 1024
    while(server_video_main.isOpened()):
        _,frame = server_video_main.read()
        frame = imutils.resize(frame, width=WIDTH)
        encoded, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 80])
        message = base64.b64encode(buffer)
        server_socket_main.sendto(message, client_addres_obligatory)
        cv2.imshow('HOST | Currently hosting', frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord('c'):
            server_socket_main.close()
            break

Client.py

client_socket_main.sendto(welcomemessage.encode(), (client_ip_adress, client_port))

while True:
    packet,_ = client_socket_main.recvfrom(BUFFOR_SIZE_DATA)
    data = base64.b85decode(packet,' /')
    npdata = np.fromstring(data, dtype=np.unit8)
    frame = cv2.imdecode(npdata, 1)
    cv2.imshow('Currently watching ', frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord('c'):
        client_socket_main.close()
        break

data = base64.b85decode(packet,' /')

TypeError: b85decode() takes 1 positional argument but 2 were given

Thanks in advance!

JUANOOO
  • 11
  • 3

1 Answers1

0

The 64KiB limit is determined not by UDP but by IP. Here's the IPv4 header from the original RFC:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Version|  IHL  |Type of Service|          Total Length         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |         Identification        |Flags|      Fragment Offset    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Time to Live |    Protocol   |         Header Checksum       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                       Source Address                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Destination Address                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

See that "Total Length" field? It's 16 bits, so its maximum value is 0xffff, which is 64Ki. That even includes headers, so the actual maximum size is smaller.

Additionally, the typical MTU on the internet is 1500 bytes, sometimes less (again, including headers). Sending packets larger than the MTU is a recipe for trouble because it means that IP fragmentation will take place, which is not desirable.

You need to break up the data into smaller chunks or use TCP which takes care of that for you.

Malt
  • 28,965
  • 9
  • 65
  • 105
  • That error message is very clear. `base64.b85decode` expects one argument, but you gave it two. – Malt Dec 01 '21 at 13:13