[Python] i am getting [WinError 10022] An invalid argument was supplied. from my udp client...

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I have two Python applications, client side and another server side.

    client side:

    import socket
    import threading

    UDP_IP = '127.0.0.1'
    UDP_PORT = 56780

    client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    nickname = input("Enter Nickname: ")
    def write():
    while True:
    print("hello")
    message = f"{nickname} -> {input()}"
    client_socket.sendto(message.encode(), (UDP_IP, UDP_PORT))

    def receive():
    while True:
    try:
    message, _ = client_socket.recvfrom(1024)
    print(message.decode())
    except Exception as e:
    print(e)
    break


    write_thread = threading.Thread(target=write)
    write_thread.start()

    receive_thread = threading.Thread(target=receive)
    receive_thread.start()


    server side:

    import socket

    HOST = '127.0.0.1'
    PORT = 56780

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_socket.bind((HOST, PORT))

    clients = set()

    def broadcast(message):
    for client in clients:
    server_socket.sendto(message, client)

    def receive():
    print("UDP Chat Server is running...")
    while True:
    try:
    message, client_address = server_socket.recvfrom(1024)
    if client_address not in clients:
    clients.add(client_address)
    print(f"Connected With {str(client_address)}")
    broadcast(message)
    except:
    clients.remove(client_address)
    receive()


    The error I am having arises from the client side application, specifically from:

    def receive():
    while True:
    try:
    message, _ = client_socket.recvfrom(1024)
    print(message.decode())
    except Exception as e:
    print(e)
    break



    The error is:


    [WinError 10022] An invalid argument was supplied
    ​

    This error only happens on my Windows system. On my Mac, the code seems to work exactly as it should.

    I tried different ports to see if any of them worked, but it did not.

    One thing I did detriment was that I get the error when I try to bind this part of the code to my server, but I can't figure out why and how to fix it.

    message, _ = client_socket.recvfrom(1024)

    Continue reading...

Compartilhe esta Página