Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <wait.h>
- #include <string.h>
- int main() {
- int server[2];
- int client[2];
- int pid;
- char text[1024];
- char temp[50];
- if (pipe(server) < 0)
- perror("Error! Nie utworzono serwera z laczem PIPE\n");
- else
- printf("Sukces! Utworzono lacze PIPE serwera\n");
- if (pipe(client) < 0)
- perror("Error! Nie utworzono klienta z laczem PIPE\n");
- else
- printf("Sukces! Utworzono lacze PIPE klienta\n");
- if ((pid = fork()) == -1)
- perror("Error! Nie moge forknac\n");
- else
- if (pid == 0) {
- close(server[0]);//odczyt
- close(client[1]);//zapis
- int status = 0;
- int lenght = read(client[0], text, sizeof(text));
- if (lenght <= 0)
- perror("\nSerwer: Blad odczytu\n");
- text[lenght - 1] = '\0';
- FILE *file = fopen(text, "r");
- if (file == NULL) {
- printf("\nSerwer: Nie odnaleziono pliku\n");
- status = -1;
- write(server[1], &status, sizeof(status));
- }
- else {
- printf("\nSerwer: Plik odnaleziony!\n");
- write(server[1], &status, sizeof(status));
- while (fgets(temp, sizeof(temp), file) != NULL) {
- write(server[1], temp, strlen(temp)+1);
- }
- }
- close(server[1]);
- close(client[0]);
- }
- else
- {
- close(server[1]);
- close(client[0]);
- printf("\nKlient: Prosze o podanie nazwy pliku: ");
- fgets(text, sizeof(text), stdin);
- int lenght = strlen(text);
- int n=0;
- if (write(client[1], text, lenght) != lenght)
- perror("\nKlient: Blad zapisu\n");
- int status;
- read(server[0], &status, sizeof(status));
- if (status == -1) {
- printf("\nKlient: Blad odczytu\n");
- }
- else
- {
- printf("\nKlient: Odczytywanie z bufora: \n\n");
- while((n=read(server[0], temp, sizeof(temp))) > 0)
- write(1, temp, n);
- }
- wait(0);
- close(server[0]);
- close(client[1]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement