The final project brings the section full circle, back to the idea from the file descriptors lesson: everything is a file. A network socket is just a file descriptor, so once two programs are connected, they talk to each other with the very same read and write you have been using on files. We will build a tiny TCP server and client that exchange one message over the network — on localhost, but the code is exactly what you would use across the internet.
socket() creates an endpoint for communication and hands you back a file descriptor for it. Once that descriptor is connected to another program, read and write move bytes across the network just as they moved bytes to and from a file. The only genuinely new material in this project is the handful of calls that set up the connection — after that, it is file I/O you already know.
Those setup calls differ for the two sides. The server waits for connections; the client reaches out to one:
The server's job is to claim an address and wait. It creates a socket, binds it to an address and port (here 127.0.0.1:48291), listens for incoming connections, and then accepts one — which blocks until a client shows up and returns a new descriptor just for that client. The SO_REUSEADDR option lets you restart the server immediately without waiting for the old port to free up:
1#include <arpa/inet.h>
2#include <stdio.h>
3#include <string.h>
htons and htonl convert the port and address into network byte order (big-endian), which is how numbers travel on the wire regardless of your machine's own endianness.
The client is simpler: create a socket and connect it to the server's address and port. There is no bind, listen, or accept — just reach out:
1#include <arpa/inet.h>
2#include <stdio.h>
3#include <unistd.h>
4
Here is the part that should feel familiar: once connected, the server writes to its client descriptor and the client reads from its socket — the identical calls from the file descriptors lesson, only the bytes now cross the network. Start the server in the background, give it a moment, then run the client:
gcc server.c -o servergcc client.c -o client./server & sleep 0.4; ./clientserver says: hello from the serverThe client received the server's message over a real TCP connection, using nothing but read and write. That is the whole point of "everything is a file" — you already knew how to do the hard part.
Our accept handles exactly one client and then the server exits. A real server loops: it calls accept over and over, and for each returned descriptor it handles that client — either by forking a child process to deal with the connection while the parent goes back to accept, or by using select/poll to watch many descriptors at once from a single process. Both are natural next steps once this version works.
That is the end of the road — from your first printf to a networked program, built entirely on the pieces in this section. Everything here feeds directly into the challenges, so when you are ready to apply it, start with the ASCII challenges.