Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----client.c-----
- /*
- Se cere un server si unul sau mai multi clienti.
- Fiecare client trimite catre server un nume de fisier text.
- Serverul citeste continutul fisierului si ii intoarce clientului
- primele trei linii in ordine alfabetica din cadrul fisierului.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- int main(int argc, char* argv[])
- {
- int ClientToServer,ServerToClient;
- char *command = malloc(strlen(argv[1])+50);
- memset(command,0,strlen(argv[1])+1);
- strcat(command,argv[1]);
- char *bufferfinal = malloc(300);
- ClientToServer = open("ClientToServer",O_WRONLY);
- ServerToClient = open("ServerToClient",O_RDONLY);
- strcpy(command,"head -n 3 ");
- strcat(command,argv[1]);
- strcat(command,"| sort");
- write(ClientToServer,command,sizeof(strlen(command) + 50));
- read(ServerToClient,bufferfinal,300);
- printf("%s \n",bufferfinal);
- free(command);
- free(bufferfinal);
- close(ClientToServer);
- close(ServerToClient);
- return 0;
- }
- ------server.c---------
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- int main()
- {
- int ClientToServer,ServerToClient;
- ClientToServer = open("ClientToServer",O_RDONLY);
- ServerToClient = open("ServerToClient",O_WRONLY);
- char *command= (char*) malloc (200);
- char *buffer1 = (char*) malloc (100);
- char *buffer2 = (char*) malloc (100);
- char *buffer3 = (char*) malloc (100);
- read(ClientToServer,command,50);
- char *bufferfinal=(char*) malloc (300);
- memset(bufferfinal,0,300);
- FILE *file = popen(command, "r");
- fgets(buffer1,100,file);
- strcpy(bufferfinal,buffer1);
- fgets(buffer2,100,file);
- strcat(bufferfinal,buffer2);
- fgets(buffer3,100,file);
- strcat(bufferfinal,buffer3);
- write(ServerToClient,bufferfinal,sizeof(strlen(bufferfinal) + 10));
- free(command);
- free(buffer1);
- free(buffer2);
- free(buffer3);
- free(bufferfinal);
- close(ClientToServer);
- close(ServerToClient);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement