read the transcript!
networking protocols are complicated
book: TCP/IP Illustrated, Volume 1, by Stevens (600 pages)
person: what if I just want to download a cat picture?
Unix systems have an API called the “socket API” that makes it easier to make network connections
Unix: you don’t need to know how TCP works. I’ll take care of it!
here’s what getting a cat picture with the Socket API looks like:
- Create a socket:
fd= socket(AF_INET, SOCK-STREAM...)
- Connect to an IP/port:
connect (fd, 22.214.171.124:80)
- Make a request:
write (fd, "GET /cat.png HTTP/I.I...)
- Read the response:
cat-picture= read (fd...)
Every HTTP library uses sockets under the hood
person: oh, cool, I could write an HTTP library too if I wanted
* SO MANY edge cases though! :)
AF_INET? What’s that?
AF-INET means basically “internet socket”: it lets you connect to other computers on the internet using their IP address.
The main alternative is AF-UNIX (“unix domain socket”) for connecting to programs on the same computer.
3 kinds of internet (AF INET) sockets:
SOCK_STREAM= TCP (curl uses this)
SOCK_DGRAM= UDP (dig (DNS) uses this)
SOCK.RAW= just let me send IP packets. I will implement my own protocol. (ping uses this)