Advertisement
Piksel

pipe

Nov 6th, 2020
2,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. void client(int,int);
  3. void server(int,int);
  4.  
  5. int main(){
  6.    
  7.     int Pipe_1[2],Pipe_2[2];
  8.    
  9.     pipe(Pipe_1);
  10.     pipe(Pipe_2);
  11.    
  12.     if (fork()==0){
  13.         close(Pipe_1[1]);
  14.         close(Pipe_2[0)];
  15.         server(Pipe_1[0], Pipe_2[1]);
  16.         exit(0);
  17.     }//if
  18.    
  19.     close(Pipe_1[0]);
  20.     close(Pipe_2[1]);
  21.    
  22.     client(Pipe_2[0],Pipe_1[1]);
  23.    
  24.     wait(0);
  25.     printf("\n");
  26.    
  27.     exit(0);
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. void client(int FdRead, int FdWrite){
  43.  
  44. char Msg[80];
  45. int n;
  46.  
  47.  
  48. spintf(Msg,"To jest tekst");
  49. printf("\n CLIENT ---> send: %s", Msg);
  50.  
  51. write(FdWrite,Msg,strlen(Msg));
  52.  
  53. while((n=read(FdRead,Msg,80)) > 0)
  54.     printf("\n Otrzymana wiadomosc od serwera: %s",Msg);
  55. return;
  56.  
  57. }//client
  58.  
  59.  
  60. void server(int FdRead, int FdWrite){
  61.  
  62. char Msg[80];
  63. int n;
  64.  
  65. if ((n=read(FdRead,Msg,80))>0)
  66. printf("\n SERVER ---> receive: %s", Msg);
  67. else
  68.     printf("\n Blad odczytu komunikatu";
  69.  
  70.     sprintf(Msg,"Hello I'm Pipe's Server and alive!!!");
  71.     printf("\n SERVER send: %s",Msg);
  72.  
  73. write(FdWrite,Msg,strlen(Msg));
  74.  
  75.  
  76.  
  77. }//servers
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement