Handle multiple requests with select

前端 未结 2 1540
悲&欢浪女
悲&欢浪女 2020-12-07 04:28

currently I am working on a chat server/client project. I am struggling with handling multiple requests with select, my server script uses the select module but the client s

相关标签:
2条回答
  • 2020-12-07 05:19

    my server script uses the select module but the client script doesn't.

    A solution is to use select also in the client. On Windows unfortunately select does not handle sys.stdin, but we can use the timeout argument to poll the keyboard.

    import socket
    import select
    import msvcrt
    client_socket = socket.socket()
    client_socket.connect(("localhost", 2855))
    msg = ""
    while True:
        ready = select.select([client_socket], [], [], .1)
        if client_socket in ready[0]:
            data = client_socket.recv(1024)
            print data, ' '*(len(msg)-len(data))
            print msg,
        if msvcrt.kbhit():
            key = msvcrt.getche()
            if key == '\r': # Enter key
                if msg == "quit":
                    break
                client_socket.send(msg)
                msg = ""
                print
            else:
                msg = msg + "" + key
    client_socket.close()
    
    0 讨论(0)
  • 2020-12-07 05:20

    There are issues on both the server and client side that prevent your application from being truly realtime. Here are several that I've noticed so far:

    1. The client is reading data from the server connection only after writing some data to the socket. Consider putting the logic to read from the socket into a separate thread.

    2. In your server, you iterate across the rlist returned by select() before sending pending messages to the client; the client fds will only be present in rlist if the client has sent a message. Sending messages should be done based on writeable fds, by iterating across wlist. But this has other problems...

    3. You always select() on writeability for all client fds, even if there's no pending messages to write to that client. The end result is that your select() call will nearly always return immediately, and will waste CPU (which sort of defeats the purpose of select)

    4. All your socket IO is done in "blocking" mode, so your send() may block if you send more data that the kernel's receive buffer on the remote end can handle (typically this is around 10MB).

    You'll be much better off using an asynchronous framework (such as twisted) for implementing this type of application. Managing all the buffers can be tedious and challenging, and it's work that's already been done before.

    0 讨论(0)
提交回复
热议问题