Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

(start_server函数)

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 :

(服务器没有运行server_socket.accept() ,我为客户端收到此错误:)

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

(OSError:[WinError 10049]请求的地址在其上下文中无效)

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?

(为什么不接受客户?)

  ask by LeoSegol translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
319 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

(您可以在下面阅读有关使用0.0.0.0的更多信息。)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...