New intermediate

Socket Programming in C: Network Programming Mastery

Learn network programming in C from scratch: sockets, TCP and UDP, getaddrinfo, poll, select, framing and serialization, with 18 lessons and tested code.

Lessons
18
Modules
6
Time
12-15 hours

Your progress

0 of 18 lessons done 0%
Start with Lesson 1
  • Free, no sign-up
  • Progress saved in this browser
  • Code you can copy and run

What you'll learn

  • Explain sockets, TCP vs UDP, IPv4/IPv6 addressing, ports and NAT
  • Handle byte order and every sockaddr struct without guesswork
  • Resolve any host for IPv4 and IPv6 with getaddrinfo and getnameinfo
  • Write TCP servers and clients: bind, listen, accept, connect, shutdown
  • Build UDP senders, dual-stack receivers and LAN broadcast discovery
  • Serve many clients in one thread with poll() and select()
  • Add timeouts, non-blocking connects and robust error handling
  • Design wire protocols: partial sends, length-prefix framing, binary serialization

Course curriculum

18 lessons in 6 modules · 12-15 hours

Module 1

Foundations

4 lessons
  1. Lesson 1: What Is a Socket? Stream vs Datagram What a socket really is in Unix, why it is just a file descriptor, and how stream (TCP) and datagram (UDP) sockets differ. Start of the C socket course. 30-40 min beginner
  2. Lesson 2: IP Addresses, Subnets and Ports IPv4 vs IPv6 addresses, CIDR subnets and netmasks, port numbers, loopback and private networks with NAT: what every socket programmer must know. 35-45 min beginner
  3. Lesson 3: Byte Order and inet_pton/inet_ntop Big-endian vs little-endian, htons/htonl/ntohs/ntohl, and converting IP address text to binary with inet_pton and inet_ntop for IPv4 and IPv6. 30-40 min beginner
  4. Lesson 4: sockaddr, sockaddr_in and addrinfo The socket address structs explained: sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage and addrinfo, plus why casting between them is safe. 35-45 min beginner

Module 2

The System Calls

5 lessons
  1. Lesson 5: getaddrinfo(): Resolving Addresses Use getaddrinfo() to turn host names and ports into socket addresses for IPv4 and IPv6, with hints, AI_PASSIVE, gai_strerror and getnameinfo. 40-50 min beginner
  2. Lesson 6: socket() and bind() in C Create sockets with socket() and attach them to a local address and port with bind(). Covers SO_REUSEADDR, 'Address already in use' and port 0. 30-40 min beginner
  3. Lesson 7: connect(), listen() and accept() How a TCP connection is made: connect() on the client, listen() and the backlog on the server, and accept() returning a new socket per client. 35-45 min beginner
  4. Lesson 8: send(), recv(), sendto(), recvfrom() Moving data over sockets in C: send() and recv() for TCP, sendto() and recvfrom() for UDP, return values, flags, SIGPIPE and detecting a closed peer. 40-50 min beginner
  5. Lesson 9: close(), shutdown() and getpeername() End TCP connections cleanly with close() and shutdown() half-close, and learn who you are talking to with getpeername(), getsockname() and gethostname(). 30-40 min beginner

Module 3

Client-Server Programs

2 lessons
  1. Lesson 10: TCP Client-Server Example in C Build a complete TCP client and server in C: a forking server that upper-cases text, a matching client, SIGCHLD reaping, and how to test it locally. 50-60 min intermediate
  2. Lesson 11: UDP Datagram Sockets in C A complete UDP example in C: a dual-stack receiver with recvfrom() and a sender with sendto(), plus packet loss, ordering and connected UDP sockets. 40-50 min intermediate

Module 4

Concurrency and I/O

3 lessons
  1. Lesson 12: Blocking Sockets and Timeouts Why socket calls block, non-blocking mode with fcntl() and O_NONBLOCK, EAGAIN, receive timeouts with SO_RCVTIMEO, and a connect() with a deadline. 40-50 min intermediate
  2. Lesson 13: poll(): Serving Many Clients Handle many TCP clients in one thread with poll(): pollfd, events and revents, a growable watch list, and a complete chat server in C. 50-60 min intermediate
  3. Lesson 14: select(): Classic I/O Multiplexing Use select() with fd_set, FD_SET, FD_ISSET and timeouts to watch sockets and the keyboard together. Includes a server console example and select's limits. 40-50 min intermediate

Module 5

Data on the Wire

3 lessons
  1. Lesson 15: Partial send() and Message Framing Fix partial send() and recv() with send_all and recv_all loops, then add message boundaries to TCP with length-prefix framing and a per-connection buffer. 50-60 min intermediate
  2. Lesson 16: Serialization: Packing Binary Data Send integers, floats and strings safely between machines: text vs binary, why raw structs break, portable big-endian packing in C, and IEEE 754 floats. 45-55 min advanced
  3. Lesson 17: UDP Broadcast Packets in C Send one UDP packet to every host on your LAN: SO_BROADCAST, limited vs subnet-directed broadcast addresses, discovery patterns, and multicast for IPv6. 30-40 min intermediate

Module 6

Reference

1 lesson
  1. Lesson 18: Socket FAQ and Quick Reference Answers to common socket programming questions (Address already in use, timeouts, Windows, raw sockets) plus a quick reference of every call and option. 40-60 min intermediate