SHARE
TWEET

Untitled

a guest Oct 21st, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  #include<stdio.h>
  2. #include<sys/wait.h>
  3. #include<sys/types.h>
  4.  #include<unistd.h>
  5.  #include<sys/types.h>
  6.  #include<sys/socket.h>
  7.  #include<netinet/in.h>
  8.  #include<arpa/inet.h>
  9.  #include<netdb.h>
  10.  #include<string.h>
  11. //#include<pthread.h>
  12.  
  13.  
  14. void childend(int signo) {
  15.     wait(NULL);
  16.     printf("omg!");
  17. }
  18.  
  19.  
  20. struct dorsz {
  21.     int cfd;
  22.     struct sockaddr_in caddr;
  23. };
  24.  
  25.  
  26. void *worker(void* arg){
  27.     struct dorsz* c = (struct dorsz*)arg;
  28.     printf("---> \033[90m %s\033[m:%d\n", inet_ntoa(c->caddr.sin_addr),
  29.             c->caddr.sin_port);  
  30.     write(c->cfd, "\033[92mhello client\n", 30);
  31.     close(c->cfd);
  32.     free(c);
  33.     return 0;
  34. }
  35.  
  36.  
  37.  
  38. int main(int argc, char** argv) {
  39.       int fd, cfd, on = 1;
  40.       socklen_t l;
  41.  
  42.       fd = socket(PF_INET, SOCK_STREAM, 0);
  43.       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
  44.       struct sockaddr_in saddr, caddr;
  45.  
  46.       signal(SIGCHLD, childend);
  47.       saddr.sin_family = PF_INET;
  48.       saddr.sin_port = htons(atoi(argv[1]));
  49.       saddr.sin_addr.s_addr = INADDR_ANY;
  50.  
  51.       bind(fd, (struct sockaddr*)&saddr, sizeof(saddr));
  52.       listen(fd, 10);
  53.       while(1) {
  54.           l = sizeof(caddr);
  55.           struct dorsz* c = malloc(sizeof(struct dorsz));
  56.           c->cfd = accept(fd, (struct sockaddr*)&caddr, &l);
  57.           pthread_t thread_id;
  58.           pthread_create(&thread_id, NULL, worker, c);
  59.           pthread_detach(thread_id);
  60.         printf("lecimy dalej");
  61.         /*  
  62.           if (fork() != 0) {
  63.               printf("SERVER");
  64.               close(cfd);
  65.         } else {
  66.           close(fd);
  67.          
  68.           //for(int i = 0; i < 100; i++)
  69.           //  write(cfd, "\a", 10);
  70.           close(cfd);
  71.           exit(0);
  72.           return 0;
  73.         }*/
  74.  
  75.  
  76.  
  77.       }
  78.       close(fd);
  79.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top