← Foundations Course

NETWORK FOUNDATIONS · 08

Port / TCP / UDP:找到 Host 後,還要找到正確 Service

IP 幫你找到 host/network destination;port 幫 transport 層把 traffic 導向某個 listening service。TCP/UDP 則提供不同 transport semantics。

Learning outcomes

1. 一台 Host 可同時有很多 Services

203.0.113.20:22   SSH
203.0.113.20:80   HTTP
203.0.113.20:443  HTTPS
203.0.113.20:3000 dev app

IP 相同,port 不同,可以對應不同 listening sockets/processes。

2. Client 也有 Port

client:
192.168.1.20:53124
      ↓ TCP
server:
203.0.113.20:443

Client 通常使用暫時的 ephemeral port,讓 OS 能區分多條同時存在的 connections。

3. TCP

connection-oriented
ordered byte stream
retransmission
flow/congestion control

Web 的 HTTP/1.1、HTTP/2 傳統上使用 TCP;HTTPS 還會加 TLS。

4. UDP

datagram-oriented
no TCP-style connection
no built-in ordered reliable stream

UDP overhead/semantics 不同,應用可自行建立需要的可靠性。像 DNS 常見 UDP;HTTP/3 則建立在 QUIC/UDP 上,不能簡化成「Web 永遠 TCP」。

5. Listener

server.listen(3000, "127.0.0.1")

Process 存在不代表它一定 listen 正確 port/interface。127.0.0.1 和 0.0.0.0 的可達範圍也不同。

Project checkpoint:Request Journey Map v8

DNS result:
203.0.113.20

URL scheme:
https
  ↓ default port 443

client socket
  ↓ transport connection
203.0.113.20:443
  ↓
service listener

Debug evidence

ss -lntp
curl http://127.0.0.1:3000

如果 local listener 都不存在,先不要查 DNS/CDN。

Knowledge check

  1. IP 和 port 各解決什麼定位問題?
  2. 為什麼 client 也需要 port?
  3. TCP/UDP 最大模型差異?
  4. process running 但 connection refused,你先查什麼?