├── README.md ├── TCP ├── TCP_client.cpp ├── TCP_server.cpp ├── client └── server ├── echoMultiServer ├── client ├── client.c ├── server └── server.c └── testFunction └── testSocket.cpp /README.md: -------------------------------------------------------------------------------- 1 | # TCP_Server 2 | 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. 3 | 4 | Screen Shot 2023-08-05 at 6 00 12 PM 5 | 6 | # TCP Server in C++ 7 | 8 | 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. 9 | 10 | ## Prerequisites 11 | 12 | Before using the TCP server, make sure you have the following: 13 | 14 | - C++ compiler (e.g., g++) 15 | - Basic understanding of networking concepts and C++ programming 16 | 17 | ## Getting Started 18 | 19 | 1. Clone this repository to your local machine: 20 | 21 | ```bash 22 | git clone https://github.com/zmoussam/TCP_Server_C- 23 | 24 | 2. Navigate to the project directory: 25 | ```bash 26 | cd TCP 27 | 28 | 3. Compile the server source code: 29 | ```bash 30 | g++ TCP_server.cpp -o server 31 | 32 | 4. Run the server: 33 | ```bash 34 | ./server 35 | 36 | ## Usage 37 | 38 | 1. Once the server is running, open another terminal window and use a TCP client (e.g., netcat) to connect to the server: 39 | Replace 9999 with the port number specified in the server.cpp file. 40 | ```bash 41 | nc localhost 9999 42 | 43 | 2. 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. 44 | 45 | 3. To exit, simply close the TCP client terminal and stop the server by pressing Ctrl + C in the terminal where the server is running 46 | 47 | ## Customization 48 | 49 | - You can modify the port number in the server.cpp file to use a different port. 50 | - Extend the server's functionality by adding more features, such as handling multiple clients simultaneously or implementing custom protocols. 51 | 52 | ## Contributing 53 | 54 | Contributions are welcome! If you find any issues or want to add improvements, feel free to open a pull request. 55 | -------------------------------------------------------------------------------- /TCP/TCP_client.cpp: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* TCP_client.cpp :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zmoussam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/14 14:35:19 by zmoussam #+# #+# */ 9 | /* Updated: 2023/07/14 16:19:25 by zmoussam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define SERVER_IP "127.0.0.1" 24 | #define PORT 8000 25 | 26 | int main() 27 | { 28 | // 1. create a socket for the client. 29 | int _clientSocket_fd = socket(AF_INET, SOCK_STREAM, 0); 30 | if(_clientSocket_fd != -1) 31 | printf("Client Socket ID: %d\n", _clientSocket_fd); 32 | else 33 | return(printf("Failed to create a socket. \n")); 34 | 35 | //2. connect the client to a specific server 36 | struct sockaddr_in serverAddress; 37 | memset(&serverAddress, 0 ,sizeof(serverAddress)); 38 | serverAddress.sin_family = AF_INET; 39 | serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP); 40 | serverAddress.sin_port = htons(PORT); 41 | 42 | if (connect(_clientSocket_fd, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == 0) 43 | printf("Connected successfully.\n"); 44 | else 45 | return(printf("Faild to connect.\n")); 46 | 47 | 48 | //3. send data to the server 49 | char buffer[1024] = "Hello, i am Zakaria\n"; 50 | 51 | send(_clientSocket_fd, buffer, strlen(buffer), 0); 52 | 53 | //4. receieve data from the server 54 | memset(buffer, 0, 1024); 55 | recv(_clientSocket_fd, buffer, 1024, 0); 56 | printf("%s\n", buffer); 57 | close(_clientSocket_fd); 58 | } -------------------------------------------------------------------------------- /TCP/TCP_server.cpp: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* TCP_server.cpp :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zmoussam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/14 15:34:30 by zmoussam #+# #+# */ 9 | /* Updated: 2023/07/14 17:54:14 by zmoussam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #define PORT 8000 23 | int main() 24 | { 25 | //1. create a socket for the server 26 | 27 | int _serverSocket_fd = socket(AF_INET, SOCK_STREAM, 0); 28 | if (_serverSocket_fd != -1) 29 | printf("Server Socket ID: %d\n", _serverSocket_fd); 30 | else 31 | return(printf("Faile to create a socket.\n")); 32 | 33 | //2. bind this socket to a specific port number 34 | 35 | struct sockaddr_in serverAddress; 36 | memset(&serverAddress, 0, sizeof(serverAddress)); 37 | 38 | serverAddress.sin_family = AF_INET; 39 | serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); 40 | serverAddress.sin_port = htons(PORT); 41 | 42 | if (bind(_serverSocket_fd, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == 0) 43 | printf("Server is binded to port no. 8000\n"); 44 | else 45 | return(printf("binding Failed\n")); 46 | 47 | //3. listen to the clients connection requests 48 | 49 | if (listen(_serverSocket_fd, 1) == 0) 50 | printf("listenning ...\n"); 51 | else 52 | return(printf("Failed to listen\n")); 53 | 54 | //4. accept the connection request 55 | 56 | struct sockaddr_in connectedClientAddtress; 57 | memset(&connectedClientAddtress, 0, sizeof(connectedClientAddtress)); 58 | 59 | int clientAddtLenght = 0; 60 | 61 | int _connectionServerSocket_fd = accept(_serverSocket_fd, (struct sockaddr *)&connectedClientAddtress, (socklen_t *)&clientAddtLenght); 62 | 63 | if (_connectionServerSocket_fd == -1) 64 | return(printf("Failed to accept a connection request\n")); 65 | else 66 | printf("Accept a request at socket ID: %d\n", _connectionServerSocket_fd); 67 | 68 | //5. send or reciever data from the clients 69 | 70 | char recievedMsg[1024]; 71 | recv(_connectionServerSocket_fd, recievedMsg, 1024, 0); 72 | printf("recieved msg : %s\n", recievedMsg); 73 | 74 | //6. close the socket 75 | 76 | close(_serverSocket_fd); 77 | close(_connectionServerSocket_fd); 78 | } -------------------------------------------------------------------------------- /TCP/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zmoussam/TCP-Server-C/06abc7756d05688e8cb0530c65a3517fe126b6b4/TCP/client -------------------------------------------------------------------------------- /TCP/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zmoussam/TCP-Server-C/06abc7756d05688e8cb0530c65a3517fe126b6b4/TCP/server -------------------------------------------------------------------------------- /echoMultiServer/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zmoussam/TCP-Server-C/06abc7756d05688e8cb0530c65a3517fe126b6b4/echoMultiServer/client -------------------------------------------------------------------------------- /echoMultiServer/client.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* client.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zmoussam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/14 21:07:57 by zmoussam #+# #+# */ 9 | /* Updated: 2023/07/14 22:13:43 by zmoussam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #define TRUE 1 25 | #define FALSEE 0 26 | #define BUFFER_SIZE 50 27 | 28 | void Write_Routine(int sock, char *buf); 29 | void Read_Routine(int sock, char *buf); 30 | 31 | //void Read_Child_Process(void); 32 | 33 | int main() 34 | { 35 | int clientSocket_fd = socket(AF_INET, SOCK_STREAM, 0); 36 | if(clientSocket_fd != -1) 37 | { 38 | printf("client's Socket: %d\n", clientSocket_fd); 39 | } 40 | else 41 | { 42 | return(printf("Error in socket Function, Line 37\n")); 43 | } 44 | 45 | struct sockaddr_in passivServerAddress; 46 | memset(&passivServerAddress, 0, sizeof(passivServerAddress)); 47 | 48 | passivServerAddress.sin_family = AF_INET; 49 | passivServerAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 50 | passivServerAddress.sin_port = htons(4444); 51 | 52 | if(connect(clientSocket_fd, (struct sockaddr *)&passivServerAddress, sizeof(passivServerAddress)) != 0) 53 | { 54 | printf("Error connect() line : 38\n"); 55 | exit(1); 56 | } 57 | else 58 | printf("Welcome to our echo system.\n"); 59 | 60 | char buffer[BUFFER_SIZE]; 61 | 62 | pid_t pid = fork(); 63 | if (pid == 0) 64 | Write_Routine(clientSocket_fd, buffer); 65 | else 66 | Read_Routine(clientSocket_fd, buffer); 67 | } 68 | 69 | void Write_Routine(int sock, char * buf) 70 | { 71 | while(1) 72 | { 73 | scanf("%s", buf); 74 | send(sock, buf, BUFFER_SIZE, 0); 75 | if (*buf == 'q' || *buf == 'Q') 76 | { 77 | close(sock); 78 | printf("Client is disconnected\n"); 79 | return; 80 | } 81 | memset(buf, 0, BUFFER_SIZE); 82 | } 83 | } 84 | 85 | void Read_Routine(int sock, char * buf) 86 | { 87 | while(1) 88 | { 89 | recv(sock, buf, BUFFER_SIZE, 0); 90 | if (strlen(buf) <= 0) 91 | { 92 | close(sock); 93 | return ; 94 | } 95 | printf("Message from the server: %s\n", buf); 96 | memset(buf, 0, BUFFER_SIZE); 97 | } 98 | } -------------------------------------------------------------------------------- /echoMultiServer/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zmoussam/TCP-Server-C/06abc7756d05688e8cb0530c65a3517fe126b6b4/echoMultiServer/server -------------------------------------------------------------------------------- /echoMultiServer/server.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* server.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zmoussam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/14 21:08:00 by zmoussam #+# #+# */ 9 | /* Updated: 2023/07/14 22:17:14 by zmoussam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #define TRUE 1 25 | #define FALSEE 0 26 | #define BUFFER_SIZE 50 27 | 28 | int main() 29 | { 30 | int serverSocket_fd = socket(AF_INET, SOCK_STREAM, 0); 31 | if(serverSocket_fd != -1) 32 | { 33 | // 34 | } 35 | else 36 | { 37 | printf("Error in socket Function, Line 37\n"); 38 | } 39 | 40 | int option = TRUE; 41 | setsockopt(serverSocket_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option)); 42 | 43 | struct sockaddr_in serverAddress; 44 | memset(&serverAddress, 0, sizeof(serverAddress)); 45 | 46 | serverAddress.sin_family = AF_INET; 47 | serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); 48 | serverAddress.sin_port = htons(4444); 49 | 50 | if(bind(serverSocket_fd, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == 0) 51 | printf("Server is binded to port no. 4444\n"); 52 | else 53 | return(printf("Error in bind() Function , line 53\n")); 54 | if (listen(serverSocket_fd, 4) == 0) 55 | printf("listenning ... \n"); 56 | else 57 | return(printf("Errot in listen() Function , line 65\n")); 58 | 59 | struct sockaddr_in connectionClientAddress; 60 | int clientAddrLength = 0; 61 | while(1) 62 | { 63 | memset(&connectionClientAddress, 0, sizeof(connectionClientAddress)); 64 | int connectionServerSock_fd = accept(serverSocket_fd, (struct sockaddr *)&connectionClientAddress, &clientAddrLength); 65 | 66 | if (connectionServerSock_fd == -1) 67 | { 68 | printf("Error in accept() Function, Line 82\n"); 69 | continue; 70 | } 71 | else 72 | printf("New client with is connected\n"); 73 | 74 | pid_t pid = fork(); 75 | if (pid == -1) 76 | { 77 | close(connectionServerSock_fd); 78 | continue; 79 | } 80 | if (pid == 0) 81 | { 82 | close(serverSocket_fd); 83 | char buffer[BUFFER_SIZE]; 84 | while (1) 85 | { 86 | memset(buffer, 0, BUFFER_SIZE); 87 | recv(connectionServerSock_fd, buffer, BUFFER_SIZE, 0); 88 | printf("%s\n", buffer); 89 | if (buffer[0] == 'q' || buffer[0] == 'Q') 90 | { 91 | printf("Client is disconnected\n"); 92 | close(connectionServerSock_fd); 93 | exit(27); 94 | } 95 | else 96 | send(connectionServerSock_fd, buffer, strlen(buffer), 0); 97 | } 98 | 99 | } 100 | else 101 | close(connectionServerSock_fd); 102 | } 103 | } -------------------------------------------------------------------------------- /testFunction/testSocket.cpp: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* testSocket.cpp :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zmoussam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/11 20:01:55 by zmoussam #+# #+# */ 9 | /* Updated: 2023/07/11 23:11:31 by zmoussam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #define PORT 8000 20 | int main() 21 | { 22 | int _clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 23 | std::cout << "the socket descriptor is : " << _clientSocket << std::endl; 24 | struct sockaddr_in sockAddress; 25 | 26 | memset(&sockAddress, 0, sizeof(sockAddress)); 27 | sockAddress.sin_family = AF_INET; // Define the used domain 28 | sockAddress.sin_port = htons(PORT); 29 | sockAddress.sin_addr.s_addr = htonl(INADDR_ANY); 30 | 31 | bind(_clientSocket, (struct sockaddr *)&sockAddress, sizeof(sockAddress)); 32 | char buffer[10]; 33 | if (read(_clientSocket, buffer, 1) < 0) 34 | std::cout << "error !" << std::endl; 35 | std::cout << buffer << std::endl; 36 | close(_clientSocket); 37 | } --------------------------------------------------------------------------------