
I left the Beej book in the bookmarks for over 3 years (bruh). I also have plans to sign up for Distributed Systems and Advanced Operating Systems next semester. As a two-birds-one-stone moment, I'll just finish this book NOW - also an appropriate time to finish this book since I'll be alot busier further down the semester.
I got inspired by an Instragram reel of someone recording their timelapse learning the C++ Primer book, so I also filmed a timelapse of studying the book (totaling to 9 hours of content).
You can visit the notes at YangTuanAnh/socket_programming_beej.
A socket is a way to speak to other programs using the standard Unix file descriptors. There are all kinds of descriptors but we will focus on Internet Sockets.
Two type of sockets include the Stream Socket and the Datagram Socket. The Stream Socket sends information in order and error-free using TCP. The Datagram Socket sends packets out of order, each packet being error-free. SOCK_DGRAM is used when TCP is unavailable or when few dropped packets are fine. Some file transfer applications use the three-way handshake to request missing packets. For unreliable applications like games, videos, audio, using UDP for some dropped packets are fine to trade for speed. For chat messages, TCP is used.
When a packet is born, it is encapsulated by various headers. When it is received, it must strip the headers to reveal the data - Data encapsulation. The Layered Network Model (OSI) describes a system of network functionality - application, presentation, session, transport, network, data link, physical. For UNIX, it is closer to "application, transport, network, physical".

IP Addresses include IPv4 (made of 4 bytes, written as dots and numbers) and IPv6 (8 chunks of 2 bytes, written as hexadecimals), out of concern for running out of IPv4 addresses. Subnets divide the address into a network address and a host address, using a netmask, and denoted as a decimal behind the IP. Port Numbers are to denote the local connection to an IP addreess, if a device has multiple services.
Byte Order includes Big-Endian and Little-Endian. Network Byte Order is always Big-Endian and Host Byte Order depends on architecture. Always run an address through a function before assigning it to the network.
Structs include struct addrinfo to prep the socket address structures for subsequent use. The socket descriptor is just an int. struct sockaddr_in and struct sockaddr_in6 denotes the socket addresses for AF_INET and AF_INET6 as IPv4 and IPv6.
IP manip functions exist so we don't have to broadcast and shift. inet_pton() converts address in printable form to network binary, and inet_ntop() does the reverse.
Private networks and firewalls use Network Address Translation to map public IPs into reversed IPs, such as 10.x.x.x and fdXX:
Use IP version-agnostic functions, change assignments to IPv6 related, use IPv6 supported structs (struct sockaddr_in6 and struct in6_addr) and functions (inet_pton(), inet_ntop(), getaddrinfo()), use IPv6 multicast
Calls learned: getaddrinfo(), socket(), connect() bind(), listen(), accept(), send(), recv(), sendto(), recvto(), close(), shutdown(), getpeername(), gethostname()

All of the implementation is in here.
Implementation in server.c, tested using telnet 0.0.0.0 3490


Implementation in client.c, calling the server in server.c with ./client 0.0.0.0

Implementation in talker.c and listener.c
Let listener.c hang with ./listener and then call ./talker :: "hello world" for the listene to receive


One of favorite releases to date! I do hope this becomes a frequent format, and an opportunity to start doing other videos.
Quick-eyed viewers might noticed a release that wasn't promoted on the socials. Xả nước, xả 'vibe' để là chính mình is also one of my favorite titles for its pun reference. Go check it out!
