Python 2.6 Chat Loop Issue. Cant Receive And Send Simultaneously

前端 未结 3 1704
别那么骄傲
别那么骄傲 2021-01-25 02:54

Im trying to make a console chat program, but have a problem with my loop. I cant get input and receive the other persons input at the same time. If two message or more are sent

3条回答
  •  孤独总比滥情好
    2021-01-25 03:26

    The problem is that your recv() call blocks until some data is received, and while recv() is blocking, your program isn't checking to see if there is any input from stdin. The traditional single-threaded solution to this is to set the socket to non-blocking I/O (via EncCon.setblocking(False)) and then have your program block inside select() instead. Pass both EncCon and stdin to select() (as part of its read-socket-set argument) so that select() will return whenever either of them has some data to give to you. (Note that this method doesn't work under Windows, as windows doesn't allow select() to block on stdin :P )

提交回复
热议问题