python - blocking sockets and select -
i'm playing around sockets, , i'm having doubts using blocking sockets select.let's assume i'm tracking potential readers. right in thinking select go through first socket in list , if data available return , if not block until select's timeout expires? when other sockets in read list checked select?
let me illustrate python example simplicity:
servsock = socket.socket(socket.af_inet, socket.sock_stream) servsock.bind(("", 2500)) servsock.listen(15) servsock.setblocking(1) readlist = [servsock] while 1: (sread, swrite, sexc) = select.select(readlist, [], [] ); sock in sread: #received connect server socket if sock == servsock: (newsock, address) = servsock.accept() newsock.setblocking(1) print "i got connection ", address readlist.append(newsock) newsock.send("you're connected select server") else: recv_msg = sock.recv(5) if recv_msg == "": (host, port) = sock.getpeername() print "client %s:%s closed connection" % (host,port) sock.close() readlist.remove(sock) else: (host, port) = sock.getpeername() print "client %s:%s sent: %s "% (host,port,recv_msg)
since they're blocking sockets, select block when testing see if socket in list has data read?
the select()
c api returns list of sockets readable. assuming python's wrapper same, sread
contain multiple elements if multiple sockets readable.
a blocking socket block if there no data present, after select()
call indicated there data, can assume one call recv
not block.
Comments
Post a Comment