How does a server get notified about the HTTP request?

I have a basic understanding of how HTTP works. I understand the client (web browser) makes a request and server responds back to the request. However, the thing I don't understand is how does a web server know when the client makes a request? If someone calls me my phone rings and I get notified. Similarly how does a webserver get notified about the request?

12.2k 23 23 gold badges 72 72 silver badges 90 90 bronze badges asked Oct 31, 2014 at 4:52 raj curious raj curious 129 1 1 silver badge 5 5 bronze badges How is this not a duplicate 5 years after Super User was launched? Commented Nov 1, 2014 at 8:44

5 Answers 5

There's a lot of layers to this. And importantly, many of them are interchangeable.

For example, you can have a coax-cable network, an ethernet, or a Wi-Fi down at the physical level. HTTP works on top of all of those, but each of them has a slightly different handling of the payload being sent around.

HTTP works on top of another protocol, called TCP, which in turn more or less runs on top of yet another protocol, called IP (nowadays mostly in two variants - IPv4 and IPv6).

So the HTTP server registers an IP address (like 184.38.45.1 , or most often "any"), along with a TCP port ( 80 being the default for HTTP, but in general anything from 1 to 65535 ), with the operating system. Now, the HTTP server tells the OS to ping it when data (or another message) comes. The OS knows when that happens, because the network interface card driver tells it that. And the NIC driver is told by the NIC itself, which actually has its own software to interpret the electrical signals on the network cable (or the wireless signals in the air etc., you get the idea).

If you want to know more about how the NIC can initiate communication with the driver / OS, you might want to lookup some basic info on hardware interrupts - basically, whatever the CPU is currently doing is stopped, and the program flow switches to an interrupt handler routine - an extremely simple piece of code that takes care of notifying the system, and then immediately returns control back to the original thing the CPU was doing. In fact, it might answer you a lot of questions about the inner workings of the OS and the computer itself - like how an operating system can "steal" CPU from running applications and shuffle the CPU resources between different applications running at the same time, even if they do not coöperate.

Back to business:

In your manual telephone analogy, imagine that your phone doesn't actually ring. To know if you're having a phone call attempt, you'll have to look at the screen periodically and check. To make this easier to manage for the HTTP server (since there's already quite a few layers who do that periodical check), you can actually block on the check attempt.

So instead of checking, seeing there's nothing there and checking again, you basically keep looking at the screen all the time. However, you've basically got a whole separate system for handling this (in your case, the hearing center, which checks the air vibrations for useful information, the ring), so it doesn't actually require your attention (CPU time).

This is further improved by techniques that allow you to monitor many connections at once (IOCP). This gets closer and closer to the phone ring system - you have a room with ten thousand phones, but you only care about those that are ringing at the moment, the others aren't taking any of your attention.