A multi-client TCP chat application written in C++ using POSIX sockets. A central server accepts connections from multiple clients and broadcasts messages between them.
.
├── server.cpp - accepts connections, broadcasts messages to all clients
└── client.cpp - connects to server, sends and receives messages
- Linux or macOS
- g++ with C++20 support
g++ -std=c++20 server.cpp -o server
g++ -std=c++20 client.cpp -o clientStart the server first:
./serverServer is listening on port: 8000
Then connect clients in separate terminals:
./clientConnected to server
Say something to the server:
Open as many client terminals as you want. Messages typed in one client appear in all others.
socket() → bind() → listen() → accept() loop
A TCP socket is created, bound to port 8000, and set to accept queued connections.
Each time accept() returns a new client fd, a detached std::thread is spawned to handle that client independently. The main thread immediately loops back to accept().
thread t(eachClient, client);
t.detach();Connected client file descriptors are stored in a shared vector<int>. A std::mutex protects every read and write to prevent data races between threads.
vector<int> clients;
mutex clients_mut;When a message arrives from a client it is prefixed with the sender's fd and forwarded to every other connected client:
string response = "Client " + to_string(client) + ": " + s;
broadcast(response, client); // skips the senderrecv() returns 0 on clean disconnect and -1 on error. The client fd is closed and removed from the registry using erase-remove:
clients.erase(remove(clients.begin(), clients.end(), client), clients.end());inet_pton() converts the string IP "127.0.0.1" to binary format. connect() establishes the TCP connection to the server on port 8000.
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
connect(client_sock, (sockaddr*)&addr, sizeof(addr));The client alternates between two blocking calls:
getline(cin, message)- waits for the user to type and press enterrecv()- waits for a response from the server
user types → send() → recv() → print → repeat
Note: because the client blocks on
getlinefirst, incoming messages from other clients only appear after you send something.
All headers are standard POSIX.
| Header | Used in | Purpose |
|---|---|---|
<sys/socket.h> |
both | socket(), bind(), accept(), recv(), send(), connect() |
<netinet/in.h> |
both | sockaddr_in, INADDR_ANY, htons() |
<arpa/inet.h> |
client | inet_pton() - string IP to binary |
<unistd.h> |
both | close() |
<thread> |
server | std::thread, detach() |
<mutex> |
server | std::mutex, std::lock_guard |
<algorithm> |
server | std::remove |
- Clients are identified by file descriptor number, not a username
- Client send/receive loop is sequential - incoming messages only display after you send one
- No message framing - a long message may split across multiple
recv()calls