OSI and TCP/IP models, IP, ports, TCP vs UDP
A network is split into layers so that each solves one task: OSI has seven, TCP/IP four. An IP address points to a host and a port to a process. TCP guarantees delivery and order, UDP does not but is faster. A failure is placed on a layer: ECONNREFUSED is transport (no listener), a connection timeout is dropped packets, a 5xx is the application or a proxy, so diagnosis goes bottom up.
Networking · TCP/IP
A test fails with ECONNREFUSED. Is it an HTTP bug, an app bug or a network problem?
A test hits localhost:3000 and gets ECONNREFUSED. A newcomer files a bug: "HTTP is broken". But HTTP has not even started: the system could not open a connection. To know where to look within a minute you need a simple map: which layers exist, what breaks on each and how they differ. This lesson gives that map and shows how to use it.
How it works and why. A network is split into layers so that each solves one problem and knows nothing about its neighbours. The OSI model has seven layers, TCP/IP has four. In an interview you do not need all seven by heart: what helps is placing a problem on a layer. TLS and WebSocket have no clean place in either model and are usually counted as the application layer.
TCP/IP layers and typical symptoms
The map dictates the order of diagnosis: lower layers first ("is there a connection"), then upper ones ("what did the server say"). If a 500 came back, TCP, IP and HTTP are fine and the search is in the application. If ECONNREFUSED came back, there will be no HTTP response at all, so opening the application logs is pointless until you know whether anything listens on that port.
IP, port, socket. An IP address (IPv4 or IPv6) points to a host, a port (0-65535) to a process on it. Common ports: 80 (HTTP), 443 (HTTPS), 53 (DNS), 22 (SSH), 5432 (PostgreSQL). A connection is uniquely defined by the IP addresses and ports of both sides. localhost is 127.0.0.1 (IPv6: ::1), the "this machine only" address. A service that listens only there is unreachable from outside the container or machine, while 0.0.0.0 means "all interfaces".
TCP and UDP: what you buy and what you pay
TCP: a reliable stream
A connection, acknowledgements, ordering, congestion control. You pay with a handshake and latency on loss. HTTP/1.1, HTTP/2, WebSocket, databases.
UDP: separate datagrams
No connection, no guarantees, no ordering, minimal overhead. DNS, voice and video, games, QUIC (under HTTP/3), which builds its own reliability.
Example: the port is published but nothing answers. The app runs in a container started with -p 3000:3000, yet from the host the request ends with Empty reply from server or Connection reset. This is a transport-layer symptom, and the diagnosis goes bottom-up:
docker run -p 3000:3000 app
curl -sS http://localhost:3000/health
# curl: (52) Empty reply from server # or (56) Connection reset
docker exec <container> ss -ltn # what listens inside
# LISTEN 0 511 127.0.0.1:3000 ... # loopback only: the cause
The process listens only on 127.0.0.1 inside the container. Port publishing forwards traffic to the container's external interface, and nothing listens there. The fix in the application:
// listens on loopback inside the container only
app.listen(3000, '127.0.0.1');
// listens on all interfaces: the port can be published
app.listen(3000, '0.0.0.0');
Two subtleties. First: "works locally" without a container proves nothing, because loopback is reachable. Second: 0.0.0.0 exposes the service on every interface, so on a machine with a public address it is a security question, and outside containers it is switched on deliberately.
Limits and alternatives. Layer models are a map, not a law: QUIC, which HTTP/3 runs on, merges transport and encryption and sits on UDP; TLS straddles the border between application and transport. ping uses ICMP, not TCP: it may be blocked by a firewall while the service works. So "ping passes" does not mean "the port is open", and "ping fails" does not mean "the host is down". To check a port: nc -vz host 443 or Test-NetConnection host -Port 443 in PowerShell.
Common mistakes.
- Reading
ECONNREFUSEDas an HTTP error and going to the app logs: there was no connection, so there are no logs. - Mixing up refused (port closed, instant answer) and timeout (packets dropped, no answer): the causes and the owners of the problem differ.
- Checking availability with
pingonly: ICMP may be blocked while the port is open, or the other way round. - Starting a service on
127.0.0.1in a container or on a VM and wondering why it is invisible from outside. - Treating UDP as "unreliable, so unusable": DNS and HTTP/3 run on it, with reliability built above.
Check yourself
A CI test gets connect ETIMEDOUT to a service, while on a laptop the same request gives an instant ECONNREFUSED when the service is off. How do the two cases differ and what do they say about the network?
Answer
ECONNREFUSED means the host is reachable and nothing listens on the port: the OS answered RST at once. ETIMEDOUT means there is no answer: a firewall drops the packets or the host is unreachable. In CI the picture is about the network (rules, routing), on the laptop about the process itself. Start by checking the route and rules between the runner and the service.
A service in Docker listens on 127.0.0.1:8080 and the port is published as -p 8080:8080. Why does a request from the host fail and what should change?
Answer
Port publishing forwards traffic to the container external interface, while the process listens only on loopback inside the container. The application or its config must listen on 0.0.0.0 (or the container interface address). Check it with ss -ltn inside the container.