Advertisement
Gistrec

Linux simple TCP chat via fork

May 14th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netinet/in.h>
  4. #include <sys/socket.h>
  5.  
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10.  
  11.  
  12. #define PID_CHILD 0
  13.  
  14. #define SERVER_PORT 8080
  15. #define CLIENT_PORT 8000
  16.  
  17. const char password[] = "aabbcc";
  18.  
  19.  
  20. /**
  21.  * Функция, в которой будут ожидаться сообщения от клиента
  22.  * @param clientfd - сокет клиента
  23.  */
  24. void listenClient(int clientfd) {
  25.     printf("Клиент подключился\n");
  26.     char buf[256];
  27.  
  28.     while (true) {
  29.         int length = recv(clientfd, buf, sizeof(buf), 0);
  30.         if (length <= 0) {
  31.             printf("Клиент отключился\n");
  32.             return;
  33.         }
  34.  
  35.         buf[length] = '\0';
  36.  
  37.         if (strncmp(buf, password, strlen(password)) != 0) {
  38.             // Пароль не правильный
  39.             printf("Пароль не верный: %s\n", buf);
  40.  
  41.             sprintf(buf, "Пароль не верный");
  42.             send(clientfd, buf, strlen(buf), 0);
  43.         }else {
  44.             printf("Пароль верный: %s\n", buf);
  45.  
  46.             sprintf(buf, "Пароль верный");
  47.             send(clientfd, buf, strlen(buf), 0);
  48.         }
  49.     }
  50. }
  51.  
  52. int main() {
  53.     // Создаем сокет
  54.     int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  55.     assert(sockfd != -1 && "Error: can't create socket");
  56.     printf("Socket created\n");
  57.  
  58.     // Создаем локальный адрес
  59.     struct sockaddr_in local_addr = {
  60.             .sin_family = AF_INET,
  61.             .sin_port = htons(SERVER_PORT),
  62.             .sin_addr = { .s_addr = INADDR_ANY }
  63.     };
  64.  
  65.     // Биндим сокет
  66.     int result = bind(sockfd, (struct sockaddr *) &local_addr, sizeof(local_addr));
  67.     assert(result != -1 && "Error: can't bind socket");
  68.     printf("Socket binded\n");
  69.  
  70.  
  71.     while (true) {
  72.         // Ожидаем подключение нового клиента
  73.         result = listen(sockfd, 5);
  74.         assert(result != -1 && "Error: can't listen socket");
  75.  
  76.         struct sockaddr_in client_addr = { 0 };
  77.         socklen_t addr_len = sizeof(client_addr);
  78.  
  79.         int clientfd = accept(sockfd, (sockaddr *) &client_addr, &addr_len);
  80.         assert(result != -1 && "Error: can't accept connection");
  81.  
  82.         int pid = fork();
  83.         assert(pid != -1 && "Error: can't create child process");
  84.         if (pid == PID_CHILD) {
  85.             listenClient(clientfd);
  86.             return 0;
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. #include <stdio.h>
  122. #include <string.h>
  123. #include <assert.h>
  124. #include <sys/socket.h>
  125. #include <netinet/in.h>
  126. #include <arpa/inet.h> /** inet_addr() */
  127.  
  128.  
  129. #define SERVER_PORT 8080
  130. #define CLIENT_PORT 8000
  131.  
  132.  
  133. int main() {
  134.     // Создаем сокет IPPROTO_IP
  135.     int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  136.     assert(sockfd != -1 && "Error: can't create socket");
  137.     printf("Socket created\n");
  138.  
  139.     // Создаем локальный адрес
  140.     struct sockaddr_in local_addr = {
  141.             .sin_family = AF_INET,
  142.             .sin_port   = htons(CLIENT_PORT),
  143.             .sin_addr   = { .s_addr = INADDR_ANY }
  144.     };
  145.  
  146.     // Биндим сокет
  147.     int result = bind(sockfd, (struct sockaddr *) &local_addr ,sizeof(struct sockaddr_in));
  148.     assert(result != -1 && "Error: can't bind socket");
  149.     printf("Socket binded\n");
  150.  
  151.     // Создаем адрес сервера
  152.     struct sockaddr_in remote_addr = {
  153.             .sin_family = AF_INET,
  154.             .sin_port   = htons(SERVER_PORT),
  155.             .sin_addr   = { .s_addr = inet_addr("127.0.0.1") },
  156.     };
  157.  
  158.     result = connect(sockfd, (sockaddr*) &remote_addr, sizeof(remote_addr));
  159.     assert(result != -1 && "Error: can't connect to server");
  160.     printf("Connected to server\n");
  161.  
  162.     char buf[256];
  163.  
  164.     while (true) {
  165.         // Запрашиваем пароль
  166.         printf("Введите пароль: ");
  167.         fflush(stdout);
  168.         scanf("%s", buf);
  169.  
  170.         // Отправляем пароль на сервер
  171.         send(sockfd, buf, strlen(buf), 0);
  172.  
  173.         // Получаем ответ
  174.         int length = recv(sockfd, buf, sizeof(buf), 0);
  175.         assert(length != -1 && "Error: can't receive message");
  176.  
  177.         buf[length] = '\0';
  178.         printf("[SERVER] %s\n", buf);
  179.     }
  180.  
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement