I have a conceptual question regarding socket programming and running the select command in a while loop. Check out this excerpt from a sample echo server (written in python, but the language doesn’t matter): http://ilab.cs.byu.edu/python/select/echoserver.html
while running:
inputready,outputready,exceptready = select.select(input,[],[])
for s in inputready:
if s == server:
# handle the server socket
client, address = server.accept()
input.append(client)
elif s == sys.stdin:
# handle standard input
junk = sys.stdin.readline()
running = 0
else:
# handle all other sockets
data = s.recv(size)
if data:
s.send(data)
else:
s.close()
input.remove(s)
Question: what is the expected behavior while the process is executing code in the for loop if a new client connects and sends data at that moment? Is there a buffer of some kind that is part of the default sockets library such that on the subsequent select call it would immediately return with the value that had come in while it was processing the earlier request? Or would a new client connecting while the server is not actually waiting in the select() be ignored, and as such it is always possible (albeit unlikely, given we are talking milliseconds) for requests to be dropped?
(NOTE: I did some empirical testing on a different codebase that uses sockets and it appears that there is a buffer — if that is in fact by design, what is the typical size of this buffer?)
The kernel network stack handles new connections asynchronously to your process, and it will never drop an incoming connection unless it would exceed the listen backlog.
So unless you are receiving connections so fast that your loop is unable to keep up with them, there is no problem. (That is, it is a question of incoming connections per second, not the timing of any particular connection relative to your loop.)