Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python select不能与多个客户端一起使用_Python_Sockets_Select_Server_Client - Fatal编程技术网

Python select不能与多个客户端一起使用

Python select不能与多个客户端一起使用,python,sockets,select,server,client,Python,Sockets,Select,Server,Client,我用Python Select库构建了一个在服务器和客户端之间传输数据的项目。 当我使用两个客户端运行程序时,只有最后一个客户端能够与服务器建立连接 服务器代码:代码的一部分 IP = "127.0.0.1" PORT = 5555 def main(): print("Setting up server...") server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREA

我用Python Select库构建了一个在服务器和客户端之间传输数据的项目。 当我使用两个客户端运行程序时,只有最后一个客户端能够与服务器建立连接

服务器代码:代码的一部分

IP = "127.0.0.1"
PORT = 5555
def main():
    print("Setting up server...")
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((IP, PORT))
    server_socket.listen(5)
    print("Listening for clients...")
    client_sockets = []

    while True:
        rlist, wlist, xlist = select.select([server_socket] + client_sockets, client_sockets, [])
        for current_socket in rlist:
            if current_socket is server_socket:
                connection, client_address = current_socket.accept()
                print("New client joined!", client_address)
                client_sockets.append(connection)
    SERVER_IP = '127.0.0.1'
    SERVER_PORT = 5555
    # open socket with the server
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_socket.connect((SERVER_IP, SERVER_PORT))

    # print instructions
    print('Welcome to remote computer application. Available commands are:\n')
    print('img\nadmin\nexit')
客户代码:代码的一部分

IP = "127.0.0.1"
PORT = 5555
def main():
    print("Setting up server...")
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((IP, PORT))
    server_socket.listen(5)
    print("Listening for clients...")
    client_sockets = []

    while True:
        rlist, wlist, xlist = select.select([server_socket] + client_sockets, client_sockets, [])
        for current_socket in rlist:
            if current_socket is server_socket:
                connection, client_address = current_socket.accept()
                print("New client joined!", client_address)
                client_sockets.append(connection)
    SERVER_IP = '127.0.0.1'
    SERVER_PORT = 5555
    # open socket with the server
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_socket.connect((SERVER_IP, SERVER_PORT))

    # print instructions
    print('Welcome to remote computer application. Available commands are:\n')
    print('img\nadmin\nexit')