Networking Basics (Sockets) in C
BSD sockets are C's network API: a server socket binds an address and port, listens, and accepts connections; a client socket connects; both sides then send/recv byte streams. The calls are pure C (socket, bind, listen, accept, connect, send, recv, close) — the same vocabulary in every language's networking stack, learned once at the source.
TCP delivers ordered byte streams, not messages — send() may partially write and recv() partially fill, so protocols frame with lengths or delimiters. A blocking TCP echo pair teaches the whole lifecycle in under a hundred lines; production adds non-blocking I/O or event loops.
Server: bind, listen, accept
socket() creates, bind() claims address:port, listen() queues callers, accept() returns one connected socket per client.
Client: connect
socket() plus connect(address) establishes the stream — afterwards both ends look identical.
Streams, not messages
TCP framing is your protocol's job — length-prefix or delimit, and loop send/recv to completion.
Close both ends
close() each accepted socket plus the listener — leaked listeners hold ports hostage across restarts (SO_REUSEADDR helps).
// Server sketch (POSIX):// int srv = socket(AF_INET, SOCK_STREAM, 0);// bind(srv, ...port 8080...); listen(srv, 5);// int cli = accept(srv, ...); // blocks until a client arrives// send(cli, "hi", 2, 0); // greet the newcomerRecommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.