Advertisement
Guest User

Untitled

a guest
May 29th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. ----client.c-----
  2.  
  3.  /*
  4. Se cere un server si unul sau mai multi clienti.
  5. Fiecare client trimite catre server un nume de fisier text.
  6. Serverul citeste continutul fisierului si ii intoarce clientului
  7. primele trei linii in ordine alfabetica din cadrul fisierului.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. #include <string.h>
  16. int main(int argc, char* argv[])
  17. {
  18.     int ClientToServer,ServerToClient;
  19.     char *command = malloc(strlen(argv[1])+50);
  20.     memset(command,0,strlen(argv[1])+1);
  21.     strcat(command,argv[1]);
  22.     char *bufferfinal = malloc(300);
  23.     ClientToServer = open("ClientToServer",O_WRONLY);
  24.     ServerToClient = open("ServerToClient",O_RDONLY);
  25.     strcpy(command,"head -n 3 ");
  26.     strcat(command,argv[1]);
  27.     strcat(command,"| sort");
  28.     write(ClientToServer,command,sizeof(strlen(command) + 50));
  29.     read(ServerToClient,bufferfinal,300);
  30.     printf("%s \n",bufferfinal);
  31.     free(command);
  32.     free(bufferfinal);
  33.     close(ClientToServer);
  34.     close(ServerToClient);
  35. return 0;
  36. }
  37.  
  38.  
  39. ------server.c---------
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <string.h>
  47. int main()
  48. {
  49.     int ClientToServer,ServerToClient;
  50.     ClientToServer = open("ClientToServer",O_RDONLY);
  51.     ServerToClient = open("ServerToClient",O_WRONLY);
  52.     char *command= (char*) malloc (200);
  53.     char *buffer1 = (char*) malloc (100);
  54.     char *buffer2 = (char*) malloc (100);
  55.     char *buffer3 = (char*) malloc (100);
  56.     read(ClientToServer,command,50);
  57.     char *bufferfinal=(char*) malloc (300);
  58.     memset(bufferfinal,0,300);
  59.     FILE *file = popen(command, "r");
  60.     fgets(buffer1,100,file);
  61.     strcpy(bufferfinal,buffer1);
  62.     fgets(buffer2,100,file);
  63.     strcat(bufferfinal,buffer2);
  64.     fgets(buffer3,100,file);
  65.     strcat(bufferfinal,buffer3);
  66.     write(ServerToClient,bufferfinal,sizeof(strlen(bufferfinal) + 10));
  67.     free(command);
  68.     free(buffer1);
  69.     free(buffer2);
  70.     free(buffer3);
  71.     free(bufferfinal);
  72.     close(ClientToServer);
  73.     close(ServerToClient);
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement