LeoSegol
LeoSegol

Reputation: 110

Why can't I accept a client?

I started a simple server that connects to a client, it worked a month ago but now it doesn't.

main

def main():
    (client_socket, client_address) = start_server(('0.0.0.0', 8200))

    print("online")
    menu = """
        enter the mode wanted out of:
        write,
        random,
        cal,
        file,
        close to terminate connection"""
    menu = menu.encode()
    main_menu(client_socket, menu)
    client_socket.close()
    server_socket.close()


if __name__ == '__main__':
    main()

start_server function

def start_server(addr):
    global server_socket
    server_socket = socket.socket()
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind(addr)
    server_socket.listen(1)
    (client_socket, client_address) = server_socket.accept()
    return client_socket, client_address

The server doesnt run the server_socket.accept() and i get this error for the client :

OSError: [WinError 10049] The requested address is not valid in its context

client socket

    my_socket = socket.socket()  # creates the socket
    my_socket.connect(('0.0.0.0', 8200))  # connects to the server
    choose_mode(my_socket)  # main menu

why does it not accept the client?

Upvotes: 2

Views: 88

Answers (2)

LeoSegol
LeoSegol

Reputation: 110

I bound 0.0.0.0 and 8200 but connected to 127.0.0.1.
server
(client_socket, client_address) = start_server(('0.0.0.0', 8200))
client
my_socket.connect(('127.0.0.1', 8200)) # connects to the server

As I was explained: because 0.0.0.0 isn't a target address to connect to, and binding to 127.0.0.1 is generally too restrictive

Upvotes: 3

Tony Farias
Tony Farias

Reputation: 93

I assume you're trying to start a server in your localhost. Depending on the platform/ OS this code is running on, this address can be invalid . Perhaps this is what has changed, your underlying platform.

To avoid the issue , use

start_server((socket.gethostname(), 8200))

or

start_server(('127.0.0.1', 8200))

You can read more about using 0.0.0.0 below.

https://www.howtogeek.com/225487/what-is-the-difference-between-127.0.0.1-and-0.0.0.0/

Upvotes: 1

Related Questions