SHARE
TWEET

Untitled

a guest Oct 21st, 2019 62 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.  
  12.  
  13. #include<pthread.h>
  14.  
  15.  
  16. void childend(int signo) {
  17.     wait(NULL);
  18.     printf("omg!");
  19. }
  20.  
  21. int main(int argc, char** argv) {
  22.       int fd, cfd, on = 1;
  23.       socklen_t l;
  24.  
  25.       fd = socket(PF_INET, SOCK_STREAM, 0);
  26.       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
  27.       struct sockaddr_in saddr, caddr;
  28.  
  29.       signal(SIGCHLD, childend);
  30.       saddr.sin_family = PF_INET;
  31.       saddr.sin_port = htons(atoi(argv[1]));
  32.       saddr.sin_addr.s_addr = INADDR_ANY;
  33.  
  34.       bind(fd, (struct sockaddr*)&saddr, sizeof(saddr));
  35.       listen(fd, 10);
  36.       while(1) {
  37.           l = sizeof(caddr);
  38.           cfd = accept(fd, (struct sockaddr*)&caddr, &l);
  39.                     printf("---> \033[90m %s\033[m:%d\n", inet_ntoa(caddr.sin_addr),     caddr.sin_port);    
  40.           if (fork() != 0) {
  41.               printf("SERVER");
  42.               close(cfd);
  43.       } else {
  44.           close(fd);
  45.    
  46.           write(cfd, "\033[92mhello client\a\a\a\a\a\a\n", 30);
  47.           //for(int i = 0; i < 100; i++)
  48.           //  write(cfd, "\a", 10);
  49.           close(cfd);
  50.           exit(0);
  51.           return 0;
  52.         }
  53.  
  54.  
  55.       }
  56.       close(fd);
  57.   }
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