anutka

Untitled

Apr 5th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/socket.h>
  5. #include <stdlib.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <netdb.h>
  9. #include <signal.h>
  10. #include <limits.h>
  11. #include <sys/epoll.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14.  
  15.  
  16. volatile sig_atomic_t terminated = 0;
  17.  
  18. void handle(int signum) {
  19.     terminated = 1;
  20. }
  21.  
  22. int create_local_server(in_port_t port) {
  23.     in_addr_t addr = inet_addr("127.0.0.1");
  24.  
  25.     struct sockaddr_in hints = {
  26.         .sin_family = AF_INET,
  27.         .sin_addr.s_addr = addr,
  28.         .sin_port = port
  29.     };
  30.  
  31.     int host_socket = socket(AF_INET, SOCK_STREAM, 0);
  32.  
  33.     bind(host_socket, (struct sockaddr*) &hints, sizeof(hints));
  34.  
  35.     return host_socket;
  36. }
  37.  
  38. void add_user_fd(int epoll_fd, int user_fd) {
  39.     struct epoll_event event = {
  40.         .events = EPOLLIN,
  41.         .data = {.fd = user_fd}
  42.     };
  43.  
  44.     epoll_ctl(epoll_fd, EPOLL_CTL_ADD, user_fd, &event);  
  45. }
  46.  
  47. void change_data(char* recv, int size, char* send) {
  48.     for (int i = 0; i < size; i ++) {
  49.         if ((recv[i] <= 'z') && (recv[i] >= 'a')) {
  50.             send[i] = recv[i] + 'A' - 'a';
  51.         } else {
  52.             send[i] = recv[i];
  53.         }
  54.     }
  55. }
  56.  
  57. int main(int argc, char* argv[]) {
  58.  
  59.     struct sigaction handler = {
  60.         .sa_handler = handle
  61.     };
  62.  
  63.     sigaction(SIGTERM, &handler, NULL);
  64.  
  65.     in_port_t port = htons(atoi(argv[1]));
  66.    
  67.     int host_socket = create_local_server(port);
  68.     fcntl(host_socket, F_SETFL, fcntl(host_socket, F_GETFL) | O_NONBLOCK);
  69.  
  70.     int epoll_fd = epoll_create1(0);
  71.  
  72.     int users_num = 0;
  73.  
  74.     if (listen(host_socket, 20) >= 0) {
  75.         char recv_data[BUFSIZ];
  76.         char send_data[BUFSIZ];
  77.         while (!terminated) {
  78.             int user_socket = 0;
  79.             while ((user_socket = accept(host_socket, NULL, NULL)) > 0) {
  80.                 add_user_fd(epoll_fd, user_socket);
  81.                 users_num++;
  82.             }
  83.  
  84.             if (users_num == 0) {
  85.                 continue;
  86.             }
  87.  
  88.             struct epoll_event event;
  89.             epoll_wait(epoll_fd, &event, 1, -1);
  90.  
  91.             int user_fd = event.data.fd;
  92.          
  93.             int size_of_recv = 0;
  94.             if ((size_of_recv = read(user_fd, &recv_data, sizeof(recv_data))) == 0){
  95.                 shutdown(user_fd, SHUT_RDWR);
  96.             } else {
  97.                 printf("%s", recv_data);
  98.                 change_data(recv_data, size_of_recv, send_data);
  99.                 write(user_fd, send_data, size_of_recv);
  100.             }
  101.         }
  102.     }
  103.  
  104.     close(host_socket);
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment