Module 1 · Network Foundations · Lesson 1 of 15
How a Request Crosses the Network: OSI, TCP/IP, IP Addresses and Ports for .NET Developers
An HTTPS request crosses network layers to reach an IP address and port. HttpClient can reuse a connection instead of setting one up again.
To understand that journey, we need to separate three questions: where the request goes, which service receives it, and how the data travels.
Where do HTTP, TLS, TCP and IP fit in OSI and TCP/IP?
A network protocol is a set of rules that computers follow when exchanging data. A layer groups protocols and responsibilities so that each part does not have to solve every networking problem.
The OSI model describes networking using seven layers. The TCP/IP model groups networking responsibilities into four layers and more closely follows the protocols used on the internet. These are models for reasoning, not seven separate method calls inside HttpClient.
First, meet the protocols involved:
- IP, Internet Protocol, carries packets between networks. A packet is a unit of data sent across a network. An IP address identifies a network interface for IP communication; an interface is a device's connection to a network.
- TCP, Transmission Control Protocol, provides a reliable, ordered stream of bytes between two endpoints. It is connection-oriented: a handshake sets up the connection before application data flows, and lost data is sent again (retransmitted). An endpoint is an IP address plus a port, a number from 0 to 65535 that tells TCP or UDP which program, or which connection, on that machine should receive the data.
- UDP, User Datagram Protocol, is connectionless: it sends individual messages without setting up a connection, and it does not guarantee delivery or order or resend lost messages. It has less overhead than TCP; applications add any guarantees they need. DNS lookups and real-time audio or video use it.
- TLS, Transport Layer Security, protects communication through encryption and checks that help detect tampering. In ordinary HTTPS, it also lets the client verify the server's identity.
Here is the mapping, read from the application down toward the network:
| OSI layer | Responsibility | TCP/IP layer | .NET touch point |
|---|---|---|---|
| 7: Application | Application-level messages, such as HTTP | Application | HttpClient and ASP.NET Core |
| 6: Presentation | Data representation and protection | Application | Encoding and TLS-related APIs |
| 5: Session | Managing an ongoing exchange | Application | No exact one-to-one .NET component |
| 4: Transport | Communication between endpoints | Transport | TCP and UDP through socket APIs |
| 3: Network | Addressing and moving packets between networks | Internet | IPAddress and operating-system IP support |
| 2: Data link | Moving data over a local network connection | Link | Operating system and network adapter |
| 1: Physical | Sending signals through the medium | Link | Network hardware |
For HTTPS over TCP, TLS sits between HTTP and TCP: HTTP runs on top of TLS, and TLS runs on top of TCP. It does not fit neatly into one OSI box.
QUIC is a secure transport built over UDP; QUIC itself provides reliability and ordering. HTTP/3 runs over QUIC, a UDP-based transport, not over TCP.
An L4 load balancer spreads TCP connections or UDP flows using addresses and ports, without reading HTTP; an L7 load balancer reads HTTP and can route by host, path or headers. Proxies, load balancers and YARP covers the trade-offs.
The mistake to avoid. Do not treat every HTTPS request as HTTP over TLS over TCP. That describes HTTP/1.1 and HTTP/2 over HTTPS, but HTTP/3 uses QUIC over UDP.
In an interview, say: “HTTP is L7, TCP and UDP are L4, and IP is L3; TCP/IP groups OSI's top three layers as application and the bottom two as link. HTTPS over TCP puts TLS between HTTP and TCP, while HTTP/3 runs on QUIC over UDP. TCP is connection-oriented, reliable and ordered; UDP is connectionless, with less overhead and no delivery or ordering guarantees.”
Microsoft's network programming overview introduces .NET's layered networking APIs. Continue with HTTP/1.1 vs HTTP/2 vs HTTP/3 for the protocol differences.
What do IPv4, IPv6, loopback and wildcard addresses mean?
IPv4 uses 32-bit addresses, usually written as four decimal numbers, such as 192.168.1.20. IPv6 uses 128-bit addresses, written in hexadecimal groups separated by colons. In IPv6 text, :: can compress a run of zero groups.
Loopback means traffic that the computer sends to itself; it never goes out through a network card. Inside a container, loopback means that container itself, not the host machine. The familiar loopback addresses are 127.0.0.1 for IPv4 and ::1 for IPv6.
This program checks those facts locally; it does not send requests or open network connections.
using System.Net;
var ipv4 = IPAddress.Parse("192.168.1.20");
var ipv6 = IPAddress.Parse("2001:db8::20");
Console.WriteLine($"IPv4 bits: {ipv4.GetAddressBytes().Length * 8}");
Console.WriteLine($"IPv6 bits: {ipv6.GetAddressBytes().Length * 8}");
Console.WriteLine($"127.0.0.1 is loopback: {IPAddress.IsLoopback(IPAddress.Loopback)}");
Console.WriteLine($"::1 is loopback: {IPAddress.IsLoopback(IPAddress.IPv6Loopback)}");
Console.WriteLine($"IPv4 wildcard: {IPAddress.Any}");
Console.WriteLine($"IPv6 wildcard: {IPAddress.IPv6Any}");IPv4 bits: 32 IPv6 bits: 128 127.0.0.1 is loopback: True ::1 is loopback: True IPv4 wildcard: 0.0.0.0 IPv6 wildcard: ::
The last two lines are wildcard addresses, not addresses you should use as remote destinations.
A binding tells a server which local address and port to listen on. A wildcard address listens on all local addresses of its kind.
| Binding | Meaning |
|---|---|
127.0.0.1:5000 | Accept IPv4 connections through loopback only |
0.0.0.0:5000 | Listen on all local IPv4 interfaces |
[::1]:5000 | Accept IPv6 connections through loopback only |
[::]:5000 | Listen on all local IPv6 interfaces |
Square brackets separate an IPv6 address from the following port. Whether an IPv6 wildcard listener also accepts IPv4 connections depends on configuration and platform behavior.
Two more address terms matter in interviews:
- Private IPv4 addresses are reserved for use within private networks and are not publicly routed on the internet. The ranges are
10.0.0.0/8,172.16.0.0/12and192.168.0.0/16. - CIDR notation writes an address, a slash, and a number. The number says how many bits at the start are fixed; the remaining bits can vary. IPv4 has 32 bits, so
/24leaves eight free bits: 256 addresses./16leaves sixteen free bits: 65,536 addresses. For example,192.168.1.0/24covers192.168.1.0through192.168.1.255; that does not mean every address is assignable to a host in every environment.
The mistake to avoid. Do not change a loopback binding to a wildcard and assume the service is now reachable from anywhere. Routing, firewall rules and, in a container, port publishing can still block it; see Docker networking, localhost, DNS and ports.
In an interview, say: “127.0.0.1 is loopback only. 0.0.0.0 listens on all local IPv4 interfaces. The IPv6 wildcard is [::], but accepting IPv4 through it depends on configuration. Wildcard binding alone does not publish a service or open a firewall.”
The Kestrel endpoint documentation shows these binding formats.
What is the difference between a port, a socket and a listener?
A port is a number, not an object holding a connection. TCP and UDP have separate port spaces: TCP port 443 and UDP port 443 are different destinations. Binding port 0 asks the operating system to choose an available port; see Socket.Bind.
Well-known ports (0–1023) hold conventional assignments: HTTP on TCP 80, HTTPS on TCP 443 and HTTP/3 on UDP 443, though a service can use any configured port.
A socket is an operating-system communication endpoint that an application accesses through an API. A listener waits for incoming connections on a bound address and port. With TCP, accepting a connection gives the server a connected socket; the listening socket remains available for further arrivals. See Socket.Accept.
The mistake to avoid. Do not use “port,” “socket” and “listener” interchangeably. Many connected sockets can share a server's listening port.
In an interview, say: “An IP address identifies the network destination, and a port selects a transport endpoint there. A socket represents communication in the operating system. A TCP listener accepts connections, producing connected sockets while continuing to listen. TCP and UDP have separate port spaces.”
How can one server port serve 10,000 clients?
A TCP connection is identified by four values, called the 4-tuple: source IP, source port, destination IP and destination port.
Look at these connections toward one server. Which fields change?
| Source IP | Source port | Destination IP | Destination port |
|---|---|---|---|
192.0.2.10 | 51000 | 203.0.113.20 | 443 |
192.0.2.10 | 51001 | 203.0.113.20 | 443 |
192.0.2.11 | 51000 | 203.0.113.20 | 443 |
The first two connections differ by source port. The third differs from the first by source IP. All three share the server's address and port.
The peer is the computer at the other end of the connection. For the server, the peer endpoint is the client's IP address and port. One listener on server port 443 can therefore accept 10,000 connections with different peer endpoints. Each accepted socket does not need a new server-side temporary port. Memory, processing capacity and operating-system limits still constrain the server.
An ephemeral port is a temporary local port selected by the operating system, normally for an outbound connection. For one fixed source IP and destination IP:port, simultaneous TCP connections need distinct source ports. That is where an outbound application can run short.
Closing connections rapidly does not free their ports at once: the side that closes first keeps each connection in TIME_WAIT for a while (see TCP vs UDP).
The Windows dynamic-port documentation gives the default range, 49152–65535. On Linux, the default is usually 32768–60999, controlled by net.ipv4.ip_local_port_range. Ranges are configurable and differ across systems. This is not a universal application connection limit.
The mistake to avoid. Do not calculate a server's client capacity from its ephemeral-port range. Accepted connections share the listener's local port; outbound connections to one fixed destination face a different constraint.
In an interview, say: “A TCP connection is identified by its 4-tuple: source IP, source port, destination IP and destination port. Thousands of accepted sockets can share server port 443 because their peer endpoints differ. Outbound connections to one destination each need a distinct ephemeral source port, and many short connections can use them up while closed ones wait in TIME_WAIT.”
What happens when HttpClient sends an HTTPS request?
DNS, the Domain Name System, resolves a host name such as api.example.com into IP addresses.
For a new HTTPS connection using TCP, the simplified sequence is:
flowchart TD
D["DNS lookup"] -->|"supplies address"| T["TCP handshake"]
T -->|"establishes transport"| S["TLS handshake"]
S -->|"protects"| H["HTTP request and response"]
H -->|"may leave available"| R["Reusable connection"]
- Resolve the host. DNS supplies candidate IP addresses, possibly from a cached result rather than a fresh network lookup.
- Establish TCP. The three-way handshake is SYN from the client, SYN-ACK from the server, then ACK from the client. The client can send its first TLS message, the ClientHello, with that final ACK, so TCP setup costs about one round trip.
- Establish TLS. The client and server negotiate protection, and the client validates the server's certificate, a signed document used to prove the server's identity for that host name.
- Exchange HTTP messages. HttpClient sends the request and receives the response.
- Keep an eligible connection available. A connection pool is a collection of connections retained for later requests rather than recreated for every call.
SNI, Server Name Indication, carries the intended host name during TLS setup so the server can select the appropriate configuration and certificate. ALPN, Application-Layer Protocol Negotiation, lets the peers agree on an application protocol, such as HTTP/2 or HTTP/1.1.
By default, GetAsync completes after reading and buffering the response content, not when the first response byte arrives. HttpCompletionOption.ResponseHeadersRead returns earlier, at the headers; your code must then read or dispose the body, as connection pooling and socket exhaustion explains.
A warm request can use a suitable existing connection, skipping DNS lookup and connection setup for that request.
The mistake to avoid. Do not equate “GetAsync completed” with “the first byte arrived.”
In an interview, say: “HttpClient resolves the name, opens TCP with SYN, SYN-ACK and ACK in about one round trip, then runs the TLS handshake, with SNI naming the host and ALPN choosing the protocol. It sends the request, reads the response and keeps the connection in the pool, so a warm call skips the setup. By default, GetAsync completes only after the response body is buffered.”
Continue with TLS, HTTPS and certificates for handshake details.
Why is the first HTTPS request slower?
Round-trip time, or RTT, is the time for data to travel to the other side and a reply to return. A cold request needs connection setup; a warm request can use an established connection.
For a simplified full TLS 1.3-over-TCP exchange, estimate the time to the first response byte as:
| Work | Approximate cost |
|---|---|
| TCP handshake | 1 RTT |
| TLS 1.3 handshake | 1 RTT |
| HTTP request to first response byte | 1 RTT |
| Total for a cold request | About 3 RTTs |
| Request on an established connection | About 1 RTT |
This model assumes small messages and excludes DNS, server processing, queueing, losses and response-body transfer.
With a 40 ms RTT, that is about 120 ms cold and 40 ms warm.
The mistake to avoid. Do not promise that every HTTPS request takes exactly three RTTs. This is a simplified cold TCP and TLS 1.3 model, not a timing guarantee.
In an interview, say: “A cold TLS 1.3-over-TCP request costs about three RTTs to the first byte, one each for TCP, TLS and HTTP; a warm one costs about one. DNS, server work and, for a default GetAsync, the body download come on top. HTTP/3 saves one round trip because QUIC sets up transport and encryption together; TLS 1.3 resumption alone does not.”
Does a chatty API need lower latency or more bandwidth?
Latency is the delay in communication. Bandwidth is the amount of data a connection can carry per unit of time. More bandwidth helps move a large response, but it does not remove the travel delay of a small request.
A chatty API requires many small calls to complete one operation. Suppose ten calls depend on one another, each using an established connection with a 40 ms RTT. Network round trips alone contribute about 400 ms. Sending fewer bytes will not remove those ten waits.
Choose improvements according to the dependency:
- Reduce unnecessary calls or batch several operations into one request.
- Reuse connections to avoid repeated setup.
- Co-locate communicating services—place them closer together—to reduce network travel time.
- For independent calls, use bounded concurrency: run a limited number at once, so the waits overlap without flooding the downstream service.
The mistake to avoid. Do not recommend more bandwidth before checking whether the operation spends its time transferring data or waiting for sequential replies.
In an interview, say: “Latency often dominates a chatty API because every sequential round trip adds delay. Bandwidth mainly helps transfer volume. I would reduce or batch calls, reuse connections, place services closer together, and overlap independent calls with bounded concurrency.”
Check yourself
A server accepts 10,000 TCP connections on port 443. Which statement explains how this works?
Show answer
Correct answer: C
The TCP 4-tuple includes both IP addresses and both ports. The server address and port can stay the same while the client address or port differs.
A small request uses an established HTTPS-over-TCP connection. Which estimate best describes the network time to the first response byte, ignoring server work and losses?
Show answer
Correct answer: A
Reusing the connection avoids setup, not the request-and-response journey. Default GetAsync also waits for the response content to be read and buffered.
Going deeper
HTTP/3 changes the estimate: QUIC sets up transport and TLS together, so a cold request needs about two RTTs to the first byte. TLS 1.3 session resumption still needs one round trip; only 0-RTT early data removes it, and .NET does not send early data. Treat the model as a way to explain where delay comes from, then measure the actual protocol and workload.
Connection reuse also depends on how long the client lives: avoid creating and disposing an independently owned HttpClient for every request. The HttpClient guidelines explain the supported approaches, and HttpClient, IHttpClientFactory and SocketsHttpHandler covers those lifetime decisions.
Sources
- Network programming in .NET - .NET | Microsoft Learn: .NET's layered networking APIs and resource identification.
- Configure endpoints for Kestrel web server | Microsoft Learn: Loopback and wildcard endpoint bindings, IPv6 bracket notation and HTTP protocol configuration.
- IPAddress Class (System.Net) | Microsoft Learn: Local address representations, byte arrays, wildcard addresses and loopback checks.
- The default dynamic port range for TCP/IP has changed in Windows Vista and in Windows Server 2008 - Windows Server | Microsoft Learn: Documented Windows dynamic-port default, separate transport configurations and configurable ranges.
- HttpCompletionOption Enum (System.Net.Http) | Microsoft Learn: Buffered versus headers-only completion.
- HttpClient guidelines for .NET - .NET | Microsoft Learn: Connection reuse, connection-creation overhead, delayed TCP port release and client lifetime guidance.
- HttpClient.GetAsync Method | Microsoft Learn: Completion after the whole response has been read with default options.
- Use HTTP/3 with HttpClient | Microsoft Learn: HTTP/3 over QUIC, a UDP-based transport.
- Socket.Accept Method | Microsoft Learn: Creating a connected socket when accepting a connection.
- Socket.Bind Method | Microsoft Learn: Binding a socket and requesting an available port with port zero.
Practice
Checked before publishing: C# examples run on .NET 10, links opened, reviewed while it was written and read in full. How lessons are checked
Updated 30 Nov 2026 ·