Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.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 <signal.h>
  10. #include <wait.h>
  11.  
  12.  
  13.  
  14. void error(const char *msg)
  15. {
  16.     perror(msg);
  17.     exit(1);
  18. }
  19.  
  20. void signal_handler(int i)
  21. {
  22.     printf("\nCaught signal I have to go now...BYEEE\n");
  23.     exit(i);
  24. }
  25.  
  26. void wait_handler(int sig)
  27. {
  28.     pid_t pid;
  29.  
  30.     pid = wait(NULL);
  31.  
  32.     printf("Pid %d exited.\n", pid);
  33. }
  34.  
  35. void parse(char *vector[20], char *line){
  36.  
  37.     int i;
  38.  
  39.  
  40.     char * pch;
  41.     pch = strtok (line," ");
  42.     i=0;
  43.     while (pch != NULL)
  44.     {
  45.         vector[i]=pch;
  46.         printf ("%s\n",pch);
  47.         pch = strtok (NULL, " ");
  48.         i++;
  49.     }
  50.     printf("i = %d\n ", i);
  51.     vector[i]=NULL;
  52.     int k=0;
  53.     for(k=0; k<=i; k++) {
  54.         printf("vector %d = %s \n",k,vector[k]);
  55.     }
  56. }
  57.  
  58. int main(int argc, char *argv[]) {
  59.  
  60.     signal(SIGINT,signal_handler);
  61.     signal(SIGCHLD, wait_handler);
  62.     int sockfd,newsockfd,portno;
  63.     socklen_t clilen;
  64.     char buffer[256];
  65.     struct sockaddr_in serv_addr, cli_addr;
  66.     int n,pid,status;
  67.     char str[INET6_ADDRSTRLEN];
  68.  
  69.  
  70.     if (argc < 2)
  71.     {
  72.         fprintf(stderr,"No port provided\n");
  73.         exit(1);
  74.     }
  75.  
  76.     sockfd = socket(AF_INET,SOCK_STREAM,0);
  77.     if(sockfd < 0)
  78.         error("ERROR opening socket");
  79.  
  80.     memset((char *) &serv_addr, 0, sizeof(serv_addr));
  81.     portno = atoi(argv[1]);
  82.     serv_addr.sin_family = AF_INET;
  83.     serv_addr.sin_addr.s_addr = INADDR_ANY;
  84.     serv_addr.sin_port = htons(portno);
  85.     if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  86.         error("ERROR on binding");
  87.     listen(sockfd, 5);
  88.     clilen = sizeof(cli_addr);
  89.  
  90.     printf("Server running\n");
  91.     for(;;) {
  92.         newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  93.  
  94.         if (newsockfd < 0)
  95.             error("ERROR on accept");
  96.  
  97.         if (inet_ntop(AF_INET, &cli_addr.sin_addr, str, INET_ADDRSTRLEN) == NULL) {
  98.             fprintf(stderr, "Could not convert byte to address \n");
  99.             exit(1);
  100.         }
  101.         fprintf(stdout, "The client address is :%s\n", str);
  102.  
  103.         pid=fork();
  104.         if (pid < 0){
  105.             error("Error in fork");
  106.         }
  107.         else if (pid > 1){ // Father
  108.             close(newsockfd);
  109.         }
  110.         else if(pid == 0) {
  111.             for(;;)
  112.             {
  113.                 bzero(buffer, 256);
  114.                 n = read(newsockfd, buffer, 255);
  115.                 if (n < 0) error("ERROS reading from socket");
  116.                 if (n == 0)
  117.                 {
  118.                     printf("Client disconnected\n");
  119.                     exit(1);
  120.                 }
  121.  
  122.                 printf("Here is the message: %s\n", buffer);
  123.  
  124.                 n = write(newsockfd, "message received", 17);
  125.                 if (n < 0) error("ERROR writing to socket");
  126.  
  127.                 if ((pid=fork())==-1) /*check for error*/
  128.                 {
  129.                     error("Error with fork!");
  130.                     exit(1);
  131.                 }
  132.                 if (pid!=0) /*The parent process*/
  133.                 {
  134.                     printf("I am parent process %d\n",getpid());
  135.                     printf("Child terminated with exit code %d\n",
  136.                            status>>8);
  137.                 }
  138.                 else /*The child process*/
  139.                 {
  140.                     char line[1024];
  141.                     int i;
  142.                     char *vector[20];
  143.                     line[strcspn(line, "\n")] = 0;
  144.                     printf("\n");
  145.                     printf("You typed %s\n",buffer);
  146.                     parse(vector,buffer);
  147.                     printf("I am child Process %d \n",getpid());
  148.                     printf("and i will replace myself by %s\n",buffer);
  149.                     execvp(vector[0],vector);
  150.                     perror("execvp");
  151.                     exit(1);
  152.                 }
  153.             }
  154.           }
  155.       }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement