A TCP server listens on a well-known port (or IP address and port pair) and accepts connections from TCP clients. A TCP client initiates a connection request to a TCP server in order to setup a connection with the server. A real TCP server can accept multiple connections on a socket.
This repository contains a simple TCP server implemented in C++. The server allows users to send and receive messages from their terminal using a TCP connection. It can serve as a basic foundation for building more complex network applications or for learning about network programming with sockets.
Before using the TCP server, make sure you have the following:
- C++ compiler (e.g., g++)
- Basic understanding of networking concepts and C++ programming
-
Clone this repository to your local machine:
git clone https://github.com/zmoussam/TCP_Server_C-
-
Navigate to the project directory:
cd TCP
-
Compile the server source code:
g++ TCP_server.cpp -o server
-
Run the server:
./server
-
Once the server is running, open another terminal window and use a TCP client (e.g., netcat) to connect to the server: Replace 9999 with the port number specified in the server.cpp file.
nc localhost 9999
-
You can now send and receive messages through the terminal. Type a message and press Enter to send it to the server. The server will display incoming messages on its terminal.
-
To exit, simply close the TCP client terminal and stop the server by pressing Ctrl + C in the terminal where the server is running
- You can modify the port number in the server.cpp file to use a different port.
- Extend the server's functionality by adding more features, such as handling multiple clients simultaneously or implementing custom protocols.
Contributions are welcome! If you find any issues or want to add improvements, feel free to open a pull request.