![A stick figure smiling](/images/welcome.jpg)
Here's a preview from my zine, Bite Size Linux!! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!
![Image of a comic. To read the full HTML alt text, click "read the transcript".](/images/uploads/sockets.png)
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, 12.13.14.15: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
$curl awesome.com
Python: requests.get("yay.us")"
(sockets)
person: oh, cool, I could write an HTTP library too if I wanted*
. Neat!
*
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)
Saturday Morning Comics!
Want another comic like this in your email every Saturday? Sign up here!