Networking Basics (Sockets) in C++
C++ has no standard networking library yet (one is in progress), so socket code uses the OS API directly — Berkeley sockets on Linux/macOS, Winsock on Windows — or a portable library like Asio/Boost.Asio. The model is universal: a server socket binds an address and port, listens, and accepts connections; a client socket connects; both sides then send and recv byte streams.
Sockets are just file-descriptor-like handles, which is why the same read/write discipline (partial sends, framing messages yourself) applies. For learning, a blocking TCP echo pair teaches the whole lifecycle in under a hundred lines; production code adds non-blocking I/O or Asio's async model.
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 delivers bytes, not packets — send() may partially write and recv() partially fill, so protocols frame with lengths or delimiters.
Prefer Asio for real work
Boost.Asio (and standalone Asio) wraps sockets portably with async support — raw syscalls for learning, Asio for shipping.
// 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.