I am binding a socket to localhost:8888, in order to make a simple server.
Here is the simple python script that I used to create the server.
import os
import socket
import fcntl
tuples = socket.getaddrinfo('localhost', 8888, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
res = tuples[0]
af, socktype, proto, canonname, sockaddr = res
sock = socket.socket(af, socktype, proto)
flags = fcntl.fcntl(sock.fileno(), fcntl.F_GETFD)
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(sock.fileno(), fcntl.F_SETFD, flags)
sock.setblocking(0)
sock.bind(sockaddr)
sock.listen(128)
- Now I open this script using an interactive shell.
- Then I go to my browser, and visit http://localhost:8888, which should queue up a connection to my server.
- I repeat the last step a few more times,
each time inside a new tab.
At this point, I expect to have many connections queued up for my server.
To check this, I go into my interactive shell and I type:
sock.accept()
This works on the first try, but on subsequent tries I get Erno 11 (EAGAIN), which tells me that no other connections are queued up. However, in my browser, the little “Connecting” spinner is still going on each tab I opened! I have tried this in both Chrome and Firefox.
I my wrote my own client that connects to localhost:8888, and was able to queue up connections with no problems. What is going on here?? Can anyone please check if this experiment can be verified on their own browsers?
For convenience, here is my client:
import os
import socket
import fcntl
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 8888))
s.recv(1000)
It is likely an optimisation on the side of your browser, trying to reuse the same connection – see http://en.wikipedia.org/wiki/HTTP_persistent_connection.
If you want to make sure, sniff the traffic using wireshark, although your localhost based address may require some fancy setup.