Asynchronousx

Untitled

Apr 30th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <sys/wait.h>
  8. #include <arpa/inet.h>
  9. #include <sys/types.h>
  10. #include <netinet/in.h>
  11. #include <netinet/ip.h>
  12. #include <sys/socket.h>
  13.  
  14. #define MESSAGE_LENGHT 64
  15. #define USER_LENGHT 16
  16. #define BACKLOG 1
  17.  
  18. //Server side
  19.  
  20. int sfd,cfd;
  21. int masterpid;
  22.  
  23. static void signal_close_socket(int);
  24.  
  25. int main(int argc, char* argv[]) {
  26.  
  27.  //Init Phase:
  28.  int ssockfd, csockfd, PORT; //defining socket filedes for both server/client
  29.  char buffer[MESSAGE_LENGHT];
  30.  char myuser[USER_LENGHT] = "server1";
  31.  char otheruser[USER_LENGHT];
  32.  struct sockaddr_in saddr, caddr; //defining server/client address
  33.  struct sigaction act;
  34.  pid_t fpid, childpid = 0, sread = 0, swrite = 0;
  35.  sigset_t set;
  36.  socklen_t slen, clen; //defining server/client address lenght
  37.  
  38.  //getting the port
  39.  PORT = atoi(argv[1]);
  40.  
  41.  //defining the master
  42.  masterpid = fpid = getpid();
  43.  
  44.  //signal handling
  45.  sigfillset(&set);
  46.  act.sa_handler = &signal_close_socket;
  47.  act.sa_mask = set;
  48.  act.sa_flags = SA_RESTART;
  49.  sigaction(SIGINT, &act, 0);
  50.  
  51.  //Socket phase:
  52.  //checking if the socket is created with success.
  53.  if((ssockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  54.     perror("socket");
  55.     exit(EXIT_FAILURE);
  56.  }
  57.  
  58.  sfd = ssockfd; //assigning the server filedes to sfd.
  59.  
  60.  //Defining a name for our socket, then binding it.
  61.  //NAME DEFINING PHASE:
  62.  saddr.sin_family = AF_INET; //adding the family address
  63.  saddr.sin_port = htons(PORT); //converting port from host protocol to network protocol, then assigning to current port.
  64.  saddr.sin_addr.s_addr = htonl(INADDR_ANY); //same as above, but with the loopback address.
  65.  
  66.  
  67.  //now binding
  68.  slen = sizeof(saddr); //size of the address of the server
  69.  if(bind(ssockfd, (struct sockaddr*)&saddr, slen) == -1) {
  70.     perror("bind");
  71.     exit(EXIT_FAILURE);
  72.  }
  73.  
  74.  //defining listening phase
  75.  if(listen(ssockfd, BACKLOG) == -1) {
  76.     perror("listen");
  77.     exit(EXIT_FAILURE);
  78.  
  79.  }
  80.  
  81.  printf("Server: correctly loaded.\n");
  82.  printf("Choose your username: ");
  83.  scanf("%s",myuser);
  84.  myuser[strlen(myuser)] = '\0';
  85.  printf("Hello %s!\n",myuser);
  86.  
  87.  //loop forever
  88.  while(1) {
  89.     printf("Waiting for connections..\n");
  90.     clen = sizeof(caddr);
  91.     //accept a pending connection
  92.     if((csockfd = accept(ssockfd, (struct sockaddr*)&caddr, &clen)) == -1) {
  93.         perror("accept");
  94.         exit(EXIT_FAILURE);
  95.     } else {
  96.         send(csockfd, &myuser, strlen(myuser)+1, 0);
  97.         recv(csockfd, &otheruser, USER_LENGHT, 0);
  98.         printf("Client %s connected.\n", otheruser);
  99.     }
  100.  
  101.     cfd = csockfd; //assigning client filedes to cfd.  
  102.  
  103.     //creating two process that will manage read and write respectively
  104.     if((childpid = fork()) == 0) {
  105.         sread=getpid();
  106.     } else if (childpid > 0) {
  107.         childpid = 0;
  108.         if((childpid = fork()) == 0) {
  109.             swrite = getpid();
  110.         } else if (childpid < 0) {
  111.             perror("fork");
  112.             exit(EXIT_FAILURE);
  113.          } 
  114.       } else {
  115.         perror("fork");
  116.         exit(EXIT_FAILURE);
  117.        }   
  118.  
  119.     while(1) {
  120.         if(sread) {
  121.             recv(csockfd, &buffer, MESSAGE_LENGHT, MSG_WAITALL);
  122.             printf("%s: %s\n",otheruser,buffer);   
  123.             fflush(stdin); 
  124.         }
  125.  
  126.         if(swrite) {
  127.             scanf("%s",buffer);
  128.             send(csockfd, &buffer, MESSAGE_LENGHT, 0);
  129.             fflush(stdin);     
  130.         }      
  131.     }
  132.  
  133.   }
  134.  
  135. }
  136.  
  137.  
  138. static void signal_close_socket(int signo) {
  139.  
  140.  if(getpid() == masterpid) {
  141.     printf("\nClosing server..\n");
  142.     close(cfd);
  143.     close(sfd);
  144.     while(wait(NULL) != -1);
  145.     exit(EXIT_SUCCESS);
  146.  } else exit(EXIT_SUCCESS);
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment