Advertisement
Aaaaa988

spo2

Apr 9th, 2021
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h>
  5. #include <string.h>
  6. #include <argp.h>
  7. #include <unistd.h>
  8.  
  9. int n_opt = 10;
  10. char *ip_arg, i_opt;
  11.  
  12. struct argp_option options[] = {{NULL,      'i', "NUM", 0, "Transmission delay in seconds"},
  13.                                 {NULL, 'n', "NUM", 0, "Number of iterations"},
  14.                                 {0}};
  15.  
  16. static int parse_opt(int key, char *arg, struct argp_state *state) {
  17.     int *arg_count = state->input;
  18.  
  19.     int tmp = arg ? strtol(arg, NULL, 10) : 0;
  20.     if (tmp < 1 && (key == 'i' || key == 'n')) {
  21.         fprintf(stderr, "error parsing option -%c", key);
  22.         exit(EXIT_FAILURE);
  23.     }
  24.  
  25.     switch (key) {
  26.         case 'i':
  27.             i_opt = tmp;
  28.             break;
  29.         case 'n':
  30.             n_opt = tmp;
  31.             break;
  32.         case ARGP_KEY_ARG:
  33.             if (*arg_count > 0)
  34.                 ip_arg = arg;
  35.             (*arg_count)--;
  36.             break;
  37.         case ARGP_KEY_END:
  38.             printf("\n");
  39.             if (*arg_count > 0)
  40.                 argp_failure(state, 1, 0, "too few arguments");
  41.             else if (*arg_count < 0)
  42.                 argp_failure(state, 1, 0, "too many arguments");
  43.             break;
  44.     }
  45.     return 0;
  46. }
  47.  
  48. int main(int argc, char **argv) {
  49.     int sockfd, arg_count = 1;
  50.     struct sockaddr_in serv_addr;
  51.     char *srv_ip = NULL, *srv_port = NULL;
  52.  
  53.     struct argp argp = {options, parse_opt, "IP:PORT"};
  54.     argp_parse(&argp, argc, argv, 0, 0, &arg_count);
  55.  
  56.     for (int i = 0, n = strlen(ip_arg); i < n; i++)
  57.         if (ip_arg[i] == ':') {
  58.             srv_ip = malloc(15 * sizeof(char));
  59.             srv_port = malloc(5 * sizeof(char));
  60.             strncpy(srv_ip, ip_arg, i);
  61.             strncpy(srv_port, ip_arg + i + 1, n - i - 1);
  62.             break;
  63.         }
  64.  
  65.     if (!srv_ip || !srv_port) {
  66.         fprintf(stderr, "failed to parse ip\n");
  67.         exit(EXIT_FAILURE);
  68.     }
  69.  
  70.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  71.         fprintf(stderr, "socket failed\n");
  72.         exit(EXIT_FAILURE);
  73.     }
  74.  
  75.     serv_addr.sin_family = AF_INET;
  76.     serv_addr.sin_port = strtol(srv_port, NULL, 10);
  77.  
  78.     if (inet_pton(AF_INET, srv_ip, &serv_addr.sin_addr) <= 0) {
  79.         fprintf(stderr, "pton failed\n");
  80.         exit(EXIT_FAILURE);
  81.     }
  82.  
  83.     if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))) {
  84.         fprintf(stderr, "connect failed\n");
  85.         exit(EXIT_FAILURE);
  86.     }
  87.  
  88.  
  89.     for (int i = 0; i < n_opt; i++) {
  90.         send(sockfd, &i_opt, 1, 0);
  91.         sleep(i_opt);
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. #include <stdio.h>
  107. #include <stdlib.h>
  108. #include <sys/socket.h>
  109. #include <netinet/in.h>
  110. #include <unistd.h>
  111. #include <pthread.h>
  112.  
  113. int convertToInt(char a[1024]){
  114.     int i = 0;
  115.     int num = 0;
  116.     while (a[i] != 0)
  117.     {
  118.         num =  (a[i])  + (num * 10);
  119.         i++;
  120.     }
  121.     return num;;
  122. }
  123.  
  124. struct srv_thread_data {
  125.     int sockfd;
  126.     FILE *log_file;
  127.     pthread_mutex_t log_mutex;
  128. };
  129.  
  130. void *serverthread(void *args) {
  131.     struct srv_thread_data *data = (struct srv_thread_data *)args;
  132.     char buff[1024] = {0};
  133.  
  134.     fprintf(stdout, "new client connected. thread started\n");
  135.  
  136.     while (read(data->sockfd, buff, 1024)) {
  137.         pthread_mutex_lock(&data->log_mutex);
  138.         fprintf(stdout, "Client: %d. Data: %d\n", data->sockfd-4, convertToInt(buff));
  139.         pthread_mutex_unlock(&data->log_mutex);
  140.     }
  141.  
  142.     pthread_mutex_lock(&data->log_mutex);
  143.     fflush(data->log_file);
  144.     pthread_mutex_unlock(&data->log_mutex);
  145.  
  146.     return NULL;
  147. }
  148.  
  149. int main(int argc, char **argv) {
  150.     int sockfd;
  151.     struct sockaddr_in addr;
  152.     socklen_t addrlen = sizeof(addr);
  153.  
  154.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  155.         fprintf(stderr, "socket failed\n");
  156.         exit(EXIT_FAILURE);
  157.     }
  158.  
  159.     addr.sin_family = AF_INET;
  160.     addr.sin_addr.s_addr = INADDR_ANY;
  161.     addr.sin_port = 0;
  162.  
  163.     if (bind(sockfd, (struct sockaddr *)&addr, addrlen) < 0) {
  164.         fprintf(stderr, "bind failed\n");
  165.         exit(EXIT_FAILURE);
  166.     }
  167.  
  168.     getsockname(sockfd, (struct sockaddr *)&addr, &addrlen);
  169.     fprintf(stdout, "bound to port: %d\n", addr.sin_port);
  170.  
  171.     if (listen(sockfd, 3) < 0) {
  172.         fprintf(stderr, "listen failed\n");
  173.         exit(EXIT_FAILURE);
  174.     }
  175.  
  176.     FILE *log_file = fopen("log.txt", "w+");
  177.     pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
  178.  
  179.     while (1) {
  180.         int clientfd;
  181.         pthread_t *clthread_id = (pthread_t *)malloc(sizeof(pthread_t));
  182.  
  183.         if ((clientfd = accept(sockfd, (struct sockaddr *)&addr, &addrlen)) < 0) {
  184.             fprintf(stderr, "accept failed\n");
  185.             continue;
  186.         }
  187.  
  188.         struct srv_thread_data *thread_data = (struct srv_thread_data *)malloc(sizeof(struct srv_thread_data));
  189.         thread_data->sockfd = clientfd;
  190.         thread_data->log_file = log_file;
  191.         thread_data->log_mutex = log_mutex;
  192.  
  193.         pthread_create(clthread_id, NULL, serverthread, (void *)thread_data);
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement