CS:APP 第 11 章
contributed by < eric88525
>
鳥哥網路介紹,建議先看
video: network programming: Part 1
1. Overview
- 大部分的網路架構都是 client-server 架構
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
network 是透過有限或是無線相連的系統
- SAN (system area network)
- LAN (local area network)
- WAN (wide area network )
-
internet vs Internet
- 小寫的 i 代表互相連接的網路
- Golbal IP Internet 是最有名的 internet
2. Network
lowest level: Ethernet Segment
- 用於房間或是一層樓
- each ethernet adapter has 48-bit address(MAC address)
- host 間傳遞 bit 資料的單位為 frames
- hub 很懶惰,every host sees every bit
Bridged Ethernet Segment
- 一堆的 ethernet 可以連成 LAN
- bridge 知道要把資料傳給哪個 host
- 用於學校或公司
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
3. internets (lower case)
透過 router 來連接多個 LAN (這些 LAN 未必相同架構)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
internet protocol
- 提供了 naming scheme (命名規則),如 host address
- 每個 host 都有他專屬的 address 來識別
- 定義了傳遞機制,基本單位為 packet
- packet 含有 header(packet 資訊) 和 payload(資料)
在此架構下的傳遞情況
- PH: internet packet header
- FH: LAN frame header
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
4. Global IP Internet
- 最有名的 internet
- base on TCP/IP
- IP (Internet Protocol): provieds basic naming scheme and unrealiable delivery capability of packets from host-to-host
- UDP (Unreliable datagram protocol): process-to-process 但不穩定
- TCP (transmission control protocol): 透過 IP 來進行可靠的 process-to-process 傳輸
hardware and software organization of an internet application
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 站在程式的角度
- Hosts 映射到 32-bit IP address
- The set of IP addresses is mapped to set of identifiers called internet domain names
- 不同 host 的 process 可透過 connection 來溝通
IPv4 and IPv6
- 32 bit-ipv4 不夠用了,因此有 128 bit ipv6 替代
- IP address 被存在 IP address struct,格式為 network byte order
Network byte-order conversion functions
l: 32bits
s: 16bit
h: host
n: network
Function of converting between binary IP address and dotted decimal strings
"n": network, "p" presentation
internet domain name
Q: 要如何將 domain name 對應到 ip 呢?
A: Internet 會維護 IP address <-> domain name 的對應,這些儲存在 huge worldwide distributed database called DNS
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
查詢 DNS 可透過 nslookup
來查找
Internet connections
client 和 server 可透過 connections
互相傳輸 bytes stream , connection
的特性
- point-to-point
- Full-duplex: 雙向的
- Reliable: 資料順序不會變動
A socket is an endpoint of a connection
- Socket address = IPaddress:port
A port is a 16-bit interger that identifies a process
- Ephemeral(臨時) port: 當 client 連線時自動決定
- Well-know port: 提供特殊服務,對應表放在
/etc/services
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
5. socket
- 對於 kernel, sokcet 是連線的終點
- 對於 application 來說他就是一個 file descriptor 可以 read/write,在 linux 中萬物皆檔案

如果使用 Internet-specific socket,最後要轉形成 (struct sockaddr *)。
當初會這樣設計是希望能有個 interface 能兼容所有種類的 socket address structure。(from book p902)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
The _in suffix is short for internet, not input.
6. 程式介紹
socket
用於 client 和 server 建立 socket。
connect
用於 client ,建立和 server 的連線,成功的話 sockfd 就能進行 read / write,
bind
- 用於 server 端
- The bind function tells the kernel to associate the server’s socket address in my_addr with the socket descriptor sockfd.
listen
- 相較於 client 是主動連線, server 是等待連線。
- 此函式將 sockfd 從 active socket 轉成具有接受連線能力的 listening socket
- backlog 為提示 kernel 預計會有多少個連線,要先準備好
open_listenfd
結合了 socket
, bind
, listen
的 function
open_clientfd
把 socket 和 connect 包在一起的實做
accept
Servers wait for connection requests from clients by calling the accept function

Example code
client 端
Server 端
getaddrinfo
Given node and service, which identify an Internet host and a
service, getaddrinfo() returns one or more addrinfo structures,
each of which contains an Internet address that can be specified
in a call to bind(2) or connect(2).
Struct of addrinfo returned by getaddrinfo
前面提到 nslookup
可以找 domain 對應許多ip, getaddrinfo 回傳的 linked list 長這樣

getnameinfo
則是相反,將 socket address 轉成對應的 host 和 service
Web Servers
- web client 和 server 使用 text-based application-level protocol HTTP(Hypertext transfer protocol) 溝通。
web content
- content is a sequence of bytes with an associated MIME (Multipurpose Internet Mail Extensions) type
- content 分為 static 和 dynamic content,差別在於有沒有需要處理資料