When a client sends a request to web server. Does the web server open a new socket ?. Or is an existing open socket used for the new request ?. If it does open a socket for each new request, how is this managed since Http is a stateless protocol. Also is this same for all web servers or do different web servers handle this differently ?.
Share
Using Unix terminology (which is near universal — sockets were introduced in the BSD flavor of Unix and spread everywhere from there), pretty much any TCP network server (web- or otherwise) will have done a listen on a socket bound to a “well-known port” (typically but not necessarily port 80 for HTTP servers). When a client connects, the server is notified (in OS-dependent ways) and it can then do an accept on the listening socket, which does create a new socket.
Depending on the level of HTTP protocol in use (the normal 1.1, or the old but still used 1.0) and some headers in the request, the client may be asking for a one-use socket (which will deal with just one request and one response), or, more commonly these days, a persistent one (also known, with older terminology dating to late HTTP 1.0 days, as a “keep alive” connection). The server does not have to honor the client’s request for the connection to be persistent, but typically tries to because it makes client performance SO much better. Each server may definitely pick their own heuristics about when it’s too loaded (too many requests coming in at once) to honor requests for persistent connections.
HTTP is still stateless even when persistent connections are in use — the client may make different requests on the still-open socket, and/or try opening different sockets, and HTTP just deals with each request/response pair separately. The socket’s persistence only saves time in terms of TCP handshakes &c (since HTTP does work on top of TCP, each new TCP connection requires its own handshake, &c).