Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <wait.h>
  6. #include <string.h>
  7.  
  8. int main() {
  9. int server[2];
  10. int client[2];
  11. int pid;
  12. char text[1024];
  13. char temp[50];
  14.  
  15. if (pipe(server) < 0)
  16. perror("Error! Nie utworzono serwera z laczem PIPE\n");
  17. else
  18. printf("Sukces! Utworzono lacze PIPE serwera\n");
  19.  
  20. if (pipe(client) < 0)
  21. perror("Error! Nie utworzono klienta z laczem PIPE\n");
  22. else
  23. printf("Sukces! Utworzono lacze PIPE klienta\n");
  24.  
  25. if ((pid = fork()) == -1)
  26. perror("Error! Nie moge forknac\n");
  27. else
  28. if (pid == 0) {
  29. close(server[0]);//odczyt
  30. close(client[1]);//zapis
  31. int status = 0;
  32. int lenght = read(client[0], text, sizeof(text));
  33.  
  34. if (lenght <= 0)
  35. perror("\nSerwer: Blad odczytu\n");
  36.  
  37. text[lenght - 1] = '\0';
  38. FILE *file = fopen(text, "r");
  39.  
  40. if (file == NULL) {
  41. printf("\nSerwer: Nie odnaleziono pliku\n");
  42. status = -1;
  43. write(server[1], &status, sizeof(status));
  44. }
  45. else {
  46. printf("\nSerwer: Plik odnaleziony!\n");
  47. write(server[1], &status, sizeof(status));
  48. while (fgets(temp, sizeof(temp), file) != NULL) {
  49. write(server[1], temp, strlen(temp)+1);
  50. }
  51. }
  52. close(server[1]);
  53. close(client[0]);
  54. }
  55. else
  56. {
  57. close(server[1]);
  58. close(client[0]);
  59.  
  60. printf("\nKlient: Prosze o podanie nazwy pliku: ");
  61. fgets(text, sizeof(text), stdin);
  62. int lenght = strlen(text);
  63. int n=0;
  64.  
  65. if (write(client[1], text, lenght) != lenght)
  66. perror("\nKlient: Blad zapisu\n");
  67.  
  68. int status;
  69. read(server[0], &status, sizeof(status));
  70.  
  71. if (status == -1) {
  72. printf("\nKlient: Blad odczytu\n");
  73. }
  74. else
  75. {
  76. printf("\nKlient: Odczytywanie z bufora: \n\n");
  77. while((n=read(server[0], temp, sizeof(temp))) > 0)
  78. write(1, temp, n);
  79. }
  80. wait(0);
  81. close(server[0]);
  82. close(client[1]);
  83. }
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement