I'm creating a simple chat application in python using socket programming and i need my socket to accept unlimited amount of connections unlike this "socket.listen(100)" which is limited to 100. Please explain me what should i do to handle large amount of connections?
The number given with listen()
is the size of the backlog queue - a hint to the number of pending requests, not the number of active requests.
You will often see a value of 5
used for the backlog parameter - this is historical. Early versions of Berkeley sockets had a "feature" where any value greater than 5 just gave 5. So 5 it was. This was fixed a long time ago, but it is still not a number you need worry too much about - remember that it is a hint only.
If the backlog is exceeded the actual error given to the client is generally the catch-all ECONNREFUSED
but older systems used to have a specific backlog error.
On many systems backlog is silently truncated to SOMAXCONN
.
See also listen() ignores the backlog argument?
You want to have an accept() loop that does nothing but accept() new connections and start a new thread to handle each one. Whenever anyone tries to connect, it instantly accepts them and adds them to a list of connections.
A thread per connection that reads with a long timeout, whatever you want your session idle timeout to be. If the timeout expires, you close the socket and exit the thread.
If the server runs out of FDs, which it will if there are enough simultaneous connections, accept() will start failing with a corresponding errno: in this case you just ignore it and keep looping. Maybe you decrease the idle timeout in this situation, and put it back when accepts start to work again.
You are looking at the problem slightly wrong. With server-side sockets, you accept connections to the same socket, which are then handled by other processes/threads.
#
# Setup socket and other handling stuff here
#
while True:
conn = sock.accept()
thread.start_new_thread(handler, (conn,))
There will be a practical limit on the maximum number of sockets based on the memory your system.
See http://docs.python.org/2/library/socketserver.html. I think the last few examples (under Asynchronous Mixins) come very close to what you want to achieve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With