Learn C
#include "systems/sockets.h"

Networking Basics (Sockets) in C

#concept

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.

#how it works
1

Server: bind, listen, accept

socket() creates, bind() claims address:port, listen() queues callers, accept() returns one connected socket per client.

2

Client: connect

socket() plus connect(address) establishes the stream — afterwards both ends look identical.

3

Streams, not messages

TCP framing is your protocol's job — length-prefix or delimit, and loop send/recv to completion.

4

Close both ends

close() each accepted socket plus the listener — leaked listeners hold ports hostage across restarts (SO_REUSEADDR helps).

#example
// 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 newcomer
Output
(client receives: hi)
1socket plus bind plus listen stages a TCP server on port 8080 with a small backlog.
2accept blocks until a client connects, returning a dedicated socket for that conversation.
3send pushes two greeting bytes down the stream — the client recovers them with recv.
#use cases
Learning TCP's lifecycle with a blocking echo client/server pair
Length-framed message protocols over stream sockets
Debugging with netcat as a stand-in peer for either end
SO_REUSEADDR for rapid server restarts during development

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.