When you write a code to listen from a port, like 80 for example, what happens under the hood? Is the method the OS uses to listen is pull, or push? In other words, does the OS checks that port every x milliseconds for example?

I just don't get it. The more I think about it, the more it seems to me that it can't be anything other than pulling.

I mean, even if OS set a callback function, still something should understand that new information has arrived to call that callback function. That something still should use pull to understand the arrival of the new data.

How a port listens?

4 Answers 4


Theoretically, this depends on the specifics of the OS and network hardware. In practice, mainstream OSes and hardware use a push model based on interrupts for interaction between the hardware and the OS (and all the software controlled by the OS).

Basically, an interrupt is a very, very low-level mechanism through which peripheral hardware can signal the CPU that there is an event that needs to be handled (like a network packet having arrived, a key having been pressed, etc.). The CPU reacts to this by suspending whatever it was doing at that time and executing an interrupt handler instead.

Interrupt handling is typically done by the OS or at least started by the OS and handed off to more specific application code that has been registered as interested in a particular type of event.

  • you mean the port is a peripheral hardware which on arrival of a new network packet, sends an interrupt to CPU. OK, but how the port understands that a new network packet has arrived? Through pull mechanism? or through electricity? How? Commented Sep 12, 2011 at 10:15
  • 1
    The network card is the peripheral hardware. A port is purely a software construct of the TCP/IP protocol. The OS will react to the hardware interrupt, call the TCP/IP stack, which has some sort of registry of which port has applications listening on the. If there is no listener registered, the packet will be ignored or an error response sent. If there is a listener, it will be called. Commented Sep 12, 2011 at 10:22
  • sorry to follow the chain that strictly, :). But how the network card understands that a new packet has arrived. I'm just trying to figure out whether the original device in this chain (application, OS, hardware, network wire) uses pull, or push. Thanks for staying with me, buddy. :) Commented Sep 12, 2011 at 10:27
  • 1
    @Saeed: I'm not sure that the disctinction "pull vs. push" makes sense at the hardware level. And it depends on the details of the networking technology. For example, the firmware of an ethernet or WiFi card will have to continuously monitor all WiFi traffic until it spots a packet whose destination MAC address matches its own, at which point it will send an interrupt. I would not call that "pull" though, since the card just listenes passively. But network hardware that is based on point-to-point connections may work completely differently. Commented Sep 12, 2011 at 10:37
  • 1
    "But how the network card understands that a new packet has arrived"? Voltage. Seriously. A signal arrives either over radio or through a wire. Voltage signals activity which the card decodes as bits. It interrupts the CPU to indicate bits have arrived.
    – S.Lott
    Commented Sep 12, 2011 at 21:09

OK, I am not a Ethernet hardware engineer, but I think I can take a stab at your question. When you write code to listen from a port, the following happens:

  • Your application code blocks/sleeps/"selects"/polls until the OS signals that a packet has arrived (with an some bits in the IP header indicating that packet is associated with that particular "port" and thus with your application that is listening on that port)
  • The OS typically doesn't poll (in x86/PC architectures) for new packets, it just merrily runs along until it receives an interrupt from the Ethernet network hardware (passed along by the Ethernet network driver, the OS/hardware interface).
  • The Ethernet network hardware itself under the covers typically contains a Ethernet controller and some "receiving buffers" aka "packet buffers". The Ethernet network driver will manage the details of noticing that a buffer is full and the OS should be notified; I am not totally clear whether this is typically push or pull within the Ethernet hardware but I suspect it's interrupt driven, or some register on the chip gets set when a buffer is ready. But what sets the buffers?
  • An even lower-level portion of the Ethernet hardware fills the buffers (or DMAs packets to main memory directly). A very low-level logic actually recognizes the swings in voltage (or optical signalling) and translates those as "bits" and places the results into the packet receiving buffers (and perhaps sets register values within the Ethernet hardware). (Sidenote: back before Network Interface Cards, when people used serial/parallel port connections, a single byte coming in could generate a hardware interrupt (IRQ) to the OS; see paragraph four of http://zone.ni.com/devzone/cda/tut/p/id/4052 )
  • How does that lowest-level hardware know what constitutes a "bit"? Well, Ethernet, at least most of the older/common ones, use Manchester Coding to decode voltage swings as bits. What does that imply? Well, as that page mentions, there is an underlying assumption that each bit is transmitted in a fixed time, an implicit clock signal. (This is why Ethernet speeds in the standards are fixed at either 10Mb/s, 100Mb/s, 1000Mb/s etc. and not completely variable; both sides of the ethernet connection must agree to the transmission rate via configuration or some initial handshaking mechanism when the Ethernet cable is plugged in. There is an actual clock circuit in hardware on both sides of the wire supporting those one or more of those frequencies. Sometimes the rate of this clock is called its frequency or the signalling rate.) While I would call it sampling, not polling/pulling, it seems correct to me to say that the voltage level is sampled at every X amount of time and the decision is made whether or not a bit is 1 or 0.

So your intuition that "pulling" occurs is, at the lowest hardware level, perhaps correct... the Ethernet circuit "pulls" what the voltage level is at certain intervals. (But the term "pull"ing can be downright incorrect if one is speaking at the OS level.) Of course, if you start talking about voltage levels and exactly how things are triggered, neither electrical nor optical physics will use the terms "push" or "pull" for describing electrical fields and potentials, but let's assume we're all programmers, not physicists here. :-)


The code that listens to a port just waits for a message from the network.

Whenever the Ethernet chip receives a message, an interruption is raised. The kernel handles this interruption and forwards the message to the network driver. That message goes through the TCP/IP stack and either gets filtered out or passed to the code that listens to the port.

  • the problem is right here. How Ethernet chip receives a message? Is it just another kind of pull? Does Ethernet chip checks the voltage of the network wire on an interval basis, for example? Commented Sep 12, 2011 at 10:18
  • 4
    This is a at an electrical level. How does a lightbulb sense the state of its switch? This is not a periodical polling. Though the details are more complex in an Ethernet chip, this is basically the same.
    – mouviciel
    Commented Sep 12, 2011 at 10:24
  • "How Ethernet chip receives a message". Voltage. Electrons on a wire or radio waves in the air. The chip becomes activated by incoming voltage and collects the bits. It then interrupts the CPU to indicate the presence of bits. The CPU has to stop doing whatever else it was doing and collect the bits before more arrive. Voltage.
    – S.Lott
    Commented Sep 12, 2011 at 21:11

The Ethernet hardware will be the one polling the physical wire ( or wireless ). When it has a 'good enough' reason, it'll interrupt the processor. The processor will figure our the proper driver which will handle the interrupt. The interrupt handler will handle the event ( read/write data ). driver will also tell the Os to wakeup any waiting threads..

  • How Ethernet hardware poll physical wire? And if they poll, the actually use pull, yeah? Do they watch the wire every 10 milliseconds or so? Commented Sep 12, 2011 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.