The r_list problem returned when the select socket experiment is carried out with Python under windows.

window uses select to verify socket reading and writing, but the set of r_list file descriptors returned by select seems to be unchanged. The code is as follows. Thank you

related codes

receiver:

import select,socket
sk=socket.socket()
sk.bind(("127.0.0.1",5002))
sk.listen(5)
inputs=[sk]
outputs=[]
while True:
    r_list,w_list,e_list=select.select(inputs,outputs,inputs,1)
    print(r_list)
    print(":%d"%len(inputs))
    for sk_conn in r_list:
        if sk_conn==sk:
            conn,addr=sk.accept()
            conn.recv(1024)
            inputs.append(conn)
            print(inputs)
        else:
            try:
                rec=sk_conn.recv(1024)
            except Exception as ex:err
            else:
                print(":"+str(rec,encoding="utf-8"

sender

import socket
ck=socket.socket()
ck.connect(("127.0.0.1",5002))
ck.sendall(bytes("hello",encoding="utf-8"))
ck.close()

what result do you expect? What is the error message actually seen?

the sender sendall is closed after once, and the receiver r_list should go through the process from 0-1 (sk)-2 (sk,conn)-0, but in fact there is always a file descriptor Conn after r_list.
clipboard.png
Why is this?

Apr.26,2021

because you didn't remove it from the first parameter of select.select () after the connection was closed.

In general, the disconnected socket is moved to the "readable" state after the select.select () call, and the call recv () usually returns an empty string.

Menu