Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. socket-chat
6 min read

Project: Client/Server

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.

Sockets Are File Descriptors

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:

server client socket() bind() listen() accept() write() socket() connect() read() connection message

The Server

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:

server.c
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

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:

client.c
1#include <arpa/inet.h>
2#include <stdio.h>
3#include <unistd.h>
4

Reading and Writing

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 server$ gcc client.c -o client$ ./server & sleep 0.4; ./clientserver says: hello from the server

The 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.

Stretch: Multiple Clients

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.

4#include <unistd.h>
5
6int main(void)
7{
8 int listener = socket(AF_INET, SOCK_STREAM, 0);
9
10 int one = 1;
11 setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
12
13 struct sockaddr_in addr = { 0 };
14 addr.sin_family = AF_INET;
15 addr.sin_port = htons(48291);
16 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
17
18 bind(listener, (struct sockaddr *) &addr, sizeof(addr));
19 listen(listener, 1);
20
21 int client = accept(listener, NULL, NULL);
22 char *msg = "hello from the server\n";
23 write(client, msg, strlen(msg));
24
25 close(client);
26 close(listener);
27 return 0;
28}
5int main(void)
6{
7 int sock = socket(AF_INET, SOCK_STREAM, 0);
8
9 struct sockaddr_in addr = { 0 };
10 addr.sin_family = AF_INET;
11 addr.sin_port = htons(48291);
12 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
13
14 connect(sock, (struct sockaddr *) &addr, sizeof(addr));
15
16 char buf[64] = { 0 };
17 read(sock, buf, sizeof(buf) - 1);
18 printf("server says: %s", buf);
19
20 close(sock);
21 return 0;
22}