Advertisement
Five_NT

cin

Oct 30th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #define MSGC "Mesaj copil"
  2. #define MSGP "Mesaj parinte"
  3.  
  4. void login() {
  5.     pid_t childPID = fork();
  6.     int sockp[2];
  7.     static const int ps = 0; // parent socker
  8.     static const int cs = 1; // child socket
  9.    
  10.     char usr[64], psd[64];
  11.    
  12.     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockp) < 0)  {
  13.         perror("Err... socketpair");
  14.         exit(1);
  15.     }
  16.  
  17.     if(childPID < 0){
  18.         perror("Nu pot fork()\n");
  19.         exit(1);
  20.  
  21.     }
  22.     else if(childPID == 0) { // Copil
  23.        
  24.         if (read(sockp[cs], usr, sizeof(usr)) < 0) perror("[C] Error read(): ");
  25.         printf("Procesul cu PID %d (c) a primit '%s'\n", getpid(), usr);       
  26.         if (write(sockp[cs], MSGC, sizeof(MSGC)) < 0) perror("[C] Error write(): ");
  27.  
  28.         close(sockp[cs]);
  29.    
  30.     }
  31.     else { // Parinte
  32.  
  33.         if(write(sockp[ps], MSGP, sizeof(MSGP)) < 0) perror("[P]Error: write()");
  34.         if(read(sockp[ps], usr, 64) < 0) perror("[P] Error read(): ");
  35.        
  36.         printf("Procesul cu PID %d(p) a primit %s\n", getpid(), usr);
  37.  
  38.         close(sockp[ps]);  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement