Advertisement
Asynchronousx

Untitled

Apr 30th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 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.  
  15. #define MESSAGE_LENGHT 64
  16. #define USER_LENGHT 16
  17.  
  18. //client side
  19.  
  20. int cfd;
  21. int masterpid;
  22.  
  23. static void signal_close_handler(int);
  24.  
  25. int main(int argc, char* argv[]) {
  26.  
  27.  //INIT PHASE
  28.  int sockfd,res,PORT;
  29.  char buffer[MESSAGE_LENGHT];
  30.  char myuser[USER_LENGHT];
  31.  char otheruser[USER_LENGHT];
  32.  pid_t fpid, childpid = 0, sread = 0, swrite = 0;
  33.  socklen_t clen;
  34.  sigset_t set;
  35.  struct sockaddr_in CLIENT_ADDRESS;
  36.  struct sigaction act;
  37.  
  38.  //defining the port
  39.  PORT = atoi(argv[1]);
  40.  
  41.  //defining the master pid
  42.  masterpid = fpid = getpid();
  43.  
  44.  //handling
  45.  sigfillset(&set);
  46.  act.sa_handler = &signal_close_handler;
  47.  act.sa_mask = set;
  48.  act.sa_flags = SA_RESTART;
  49.  sigaction(SIGINT, &act, 0);
  50.  
  51.  
  52.  //creating a new socket using NETINET TCP/IP protocol
  53.  if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  54.     perror("ops");
  55.     exit(EXIT_FAILURE);
  56.  }
  57.  
  58.  //now defining the socket address name
  59.  CLIENT_ADDRESS.sin_family = AF_INET;
  60.  CLIENT_ADDRESS.sin_port = htons(PORT);
  61.  CLIENT_ADDRESS.sin_addr.s_addr = inet_addr("192.168.1.68");
  62.  
  63.  //defining the username
  64.  printf("Welcome to the chatting system.\n");
  65.  printf("Choose a username: ");
  66.  scanf("%s", myuser);
  67.  myuser[strlen(myuser)] = '\0';
  68.  printf("Hello, %s!\n",myuser);
  69.  
  70.  //trying to connect
  71.  printf("Waiting for the server response..\n");
  72.  
  73.  clen = sizeof(CLIENT_ADDRESS);
  74.  if((res = connect(sockfd, (struct sockaddr*)&CLIENT_ADDRESS, clen)) == -1) {
  75.     perror("ops");
  76.     exit(EXIT_FAILURE);
  77.  } else {
  78.     printf("Welcome to the server, %s!\n",myuser);
  79.     recv(sockfd, &otheruser, USER_LENGHT, 0);
  80.     send(sockfd, &myuser, strlen(myuser)+1, 0);
  81.     printf("chatting with: %s\n",otheruser);
  82.    }
  83.  
  84.  //chat phase
  85.  while(1) {
  86.     //creating two process that will manage read and write respectively
  87.     if((childpid = fork()) == 0) {
  88.         sread=getpid();
  89.     } else if (childpid > 0) {
  90.         childpid = 0;
  91.         if((childpid = fork()) == 0) {
  92.             swrite = getpid();
  93.         } else if (childpid < 0) {
  94.             perror("fork");
  95.             exit(EXIT_FAILURE);
  96.          } 
  97.       } else {
  98.         perror("fork");
  99.         exit(EXIT_FAILURE);
  100.        }
  101.  
  102.     //until the user press CTRL+C
  103.     while(1) {
  104.         if(sread) {
  105.             recv(sockfd, &buffer, MESSAGE_LENGHT, MSG_WAITALL);
  106.             printf("%s: %s\n",otheruser, buffer);
  107.             fflush(stdin);     
  108.         }
  109.  
  110.         if(swrite) {
  111.             scanf("%s",buffer);
  112.             send(sockfd, &buffer, MESSAGE_LENGHT, 0);  
  113.             fflush(stdin); 
  114.         }      
  115.     }
  116.  }
  117.  
  118. }
  119.  
  120.  
  121. static void signal_close_handler(int signo) {
  122.  
  123.  if(getpid() == masterpid) {
  124.     printf("\nClosing CLIENT..\n");
  125.     close(cfd);
  126.     while(wait(NULL) != -1);
  127.     exit(EXIT_SUCCESS);
  128.  } else exit(EXIT_SUCCESS);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement