0
我試圖安裝的是不阻斷接受(...)的插座,使用下面的代碼:WSAEventSelect與FD_ACCEPT,recv的返回WSAEWOULDBLOCK
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
event = win32event.CreateEvent(None, True, False, None)
win32file.WSAEventSelect(sock.fileno(), event, win32file.FD_ACCEPT)
sock.listen(5)
rc = win32event.WaitForSingleObject(event, win32event.INFINITE)
if not rc == win32event.WAIT_OBJECT_0:
return
conn, addr = sock.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
當客戶端連接,但沒有數據,recv
返回WSAEWOULDBLOCK
。閱讀MSDN解釋這是非阻塞套接字的正確行爲,但使用WSAEventSelect
時,我只指定了FD_ACCEPT
,而沒有FD_READ
。因此,我希望recv
在沒有數據時阻塞,並在連接正常關閉時返回0。
我在做什麼錯?