We Implemet an application in C++ on Linux to create the connections between multiple clients and server using TCP
-
Server Side:
1. Create the server socket based on TCP.
2. Listening on a target port (Default: 8080).
3. Support to accept multiple clients.
4. Real-time receive the clients' messages and echo back to the responding clients in time.
5. Implement this application in C/C++ on Linux, and multi-thread is applied.
-
Client Side:
1. Two options to start the client service (Default option and command line option).
2. Send the message to the server periodically (one time each second).
3. Receive the server's response in time, and print the response message on the terminal.
4. The main thread is used to deal with "send task", the child thread is used to deal with "receive/print task".
5. Implement this application in C/C++ on Linux.
git clone https://github.com/Jinxin-Xiong/TCP-Server-Clients-Socket-Application.git
cd TCP-Server-Clients-Socket-Application
sudo make
-
Service would be generated in parent directory
-
Run server before cliens
-
Ctrl + c
to interrupt blocking server and client process
- Set server IP, 127.0.0.1 is just for local test
#define SERVER_IP "127.0.0.1"
- Set server port
#define SERVER_PORT 8080
- Set the maximum number of connected clients
#define MAXCLIENTNUM 100
- Run server service on the server side:
./TCPServerClients_server
The status after you start the server service (Listen to the target port):
- Run client on client side
There are two options:
- Default option: The Server IP and Port are default set in CommonHead.h;
- Command Line option: Type Server IP and Port as commond line
/***** Option 1: Default option (Default IP: 127.0.0.1; Default Port: 8080) *****/
./TCPServerClients_client
/***** Option 2: Command Line option *****/
./TCPServerClients_client IP Port (ex. ./TCPServerClients_client 127.0.0.1 8080)
-
Tips: Since there is only one desktop in working place, I just run one server terminal and six client terminals in the same computer (local network). But generally, this program can support much more than six clients
-
In future, it would be necessary to test this program in the real server-multi client scenario: