Quoting from this socket tutorial:
Sockets come in two primary flavors.
An active socket is connected to a
remote active socket via an open data
connection… A passive socket is
not connected, but rather awaits an
incoming connection, which will
spawn a new active socket once a
connection is established
…Each port can have a single passive
socket binded to it, awaiting
incoming connections, and
multiple active sockets, each
corresponding to an open
connection on the port. It’s as if
the factory worker is waiting for new
messages to arrive (he represents
the passive socket), and when one
message arrives from a new sender, he
initiates a correspondence (a
connection) with them by
delegating someone else (an active
socket) to actually read the packet
and respond back to the sender if
necessary. This permits the factory
worker to be free to receive new
packets.
…
Then the tutorial explains that, after a connection is established, the active socket continues receiving data until there are no remaining bytes, and then closes the connection.
What I didn’t understand is this: Suppose there’s an incoming connection to the port, and the sender wants to send some little data every 20 minutes. If the active socket closes the connection when there are no remaining bytes, does the sender have to reconnect to the port every time it wants to send data? How do we persist a once established connection for a longer time? Can you tell me what I’m missing here?
My second question is, who determines the limit of the concurrently working active sockets?
The sender should send a KEEPALIVE packet at regular intervals to keep the connection alive. The format of the KEEPALIVE depends on the protocol. It could be as small as a single NULL in the TCP data segment.
As to the second question… it depends on the I/O. If it is blocking I/O then you only want a certain number of threads running on your computer, so you won’t be able to have many clients. If it’s non-blocking, you can have a lot more clients. Programming languages should have support for both blocking and non-blocking I/O. (I know for a fact that Java does.)
It also depends on things like bandwidth, the data transfer for each client, memory, clock speed, etc. But non-blocking vs. blocking can make a huge difference in the number of clients you can accept. You probably can’t have more than 5-10 clients blocking without your server crashing… but you can have thousands if you’re not blocking.