Python Server Client WinError 10057

15,123

It looks like you're confused about when you actually connect to the server, so just remember that you always bind to a local port first. Therefore, this line:

server_address = ('192.168.1.5', 4242)

Should actually read:

server_address = ('', 4242)

Then, before your infinite loop, put in the following line of code:

sock.connect(('192.168.1.5', 4242))

And you should be good to go. Good luck!

EDIT: I suppose I should be more careful with the term "always." In this case, you want to bind to a local socket first.

Share:
15,123
user3677994
Author by

user3677994

Updated on July 01, 2022

Comments

  • user3677994
    user3677994 almost 2 years

    I'm making a server and client in Python 3.3 using the socket module. My server code is working fine, but this client code is returning an error. Here's the code:

    import socket
    import sys
    import os
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_address = ('192.168.1.5', 4242)
    sock.bind(server_address)
    
    while True:
        command = sock.recv(1024)
        try:
            os.system(command)
            sock.send("yes")
        except:
            sock.send("no")
    

    And here's the error:

    Error in line: command = sock.recv(1024)
    OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected  and (when sending on a datagram socket using a sendto call) no address was supplied
    

    What on earth is going on?