Advertisement
bol0bal4

PWN 500

Apr 25th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5.  
  6. void doprocessing(int sock){
  7.  
  8.     char buff[1024];
  9.     read(sock,buff,10000);
  10. }
  11. int main( int argc, char *argv[] )
  12. {
  13.     int sockfd, newsockfd, portno, clilen,pid;
  14.     char buffer[256];
  15.     struct sockaddr_in serv_addr, cli_addr;
  16.     int  n;
  17.  
  18.     /* First call to socket() function */
  19.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  20.     if (sockfd < 0)
  21.     {
  22.         perror("ERROR opening socket");
  23.         exit(1);
  24.     }
  25.     /* Initialize socket structure */
  26.     bzero((char *) &serv_addr, sizeof(serv_addr));
  27.     portno = 5001;
  28.     serv_addr.sin_family = AF_INET;
  29.     serv_addr.sin_addr.s_addr = INADDR_ANY;
  30.     serv_addr.sin_port = htons(portno);
  31.  
  32.     /* Now bind the host address using bind() call.*/
  33.     if (bind(sockfd, (struct sockaddr *) &serv_addr,
  34.                           sizeof(serv_addr)) < 0)
  35.     {
  36.          perror("ERROR on binding");
  37.          exit(1);
  38.     }
  39.     /* Now start listening for the clients, here
  40.      * process will go in sleep mode and will wait
  41.      * for the incoming connection
  42.      */
  43.     listen(sockfd,5);
  44.     clilen = sizeof(cli_addr);
  45.     while (1)
  46.     {
  47.         newsockfd = accept(sockfd,
  48.                 (struct sockaddr *) &cli_addr, &clilen);
  49.         if (newsockfd < 0)
  50.         {
  51.             perror("ERROR on accept");
  52.             exit(1);
  53.         }
  54.         /* Create child process */
  55.         pid = fork();
  56.         if (pid < 0)
  57.         {
  58.             perror("ERROR on fork");
  59.         exit(1);
  60.         }
  61.         if (pid == 0)  
  62.         {
  63.             /* This is the client process */
  64.             close(sockfd);
  65.             doprocessing(newsockfd);
  66.             write(newsockfd,"FINE",4);
  67.             exit(0);
  68.         }
  69.         else
  70.         {
  71.             close(newsockfd);
  72.         }
  73.     } /* end of while */
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement