Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <netdb.h>
  2. #include <netinet/in.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6.  
  7. int fileSEND(char *server, int PORT, char *lfile, char *rfile){
  8.  
  9.     int socketDESC;
  10.     struct sockaddr_in serverADDRESS;
  11.     struct hostent *hostINFO;
  12.     FILE * file_to_send;
  13.     int ch;
  14.     char toSEND[1];
  15.     char remoteFILE[4096];
  16.     int count1=1,count2=1, percent;
  17.  
  18.     hostINFO = gethostbyname(server);
  19.     if (hostINFO == NULL) {
  20.         printf("L'adresse ip du serveur n'est pas accesible\n");
  21.         return 1;
  22.     }
  23.  
  24.     socketDESC = socket(AF_INET, SOCK_STREAM, 0);
  25.     if (socketDESC < 0) {
  26.         printf("Erreur création socket\n");
  27.         return 1;
  28.     }
  29.  
  30.     serverADDRESS.sin_family = hostINFO->h_addrtype;
  31.     memcpy((char *) &serverADDRESS.sin_addr.s_addr, hostINFO->h_addr_list[0], hostINFO->h_length);
  32.     serverADDRESS.sin_port = htons(PORT);
  33.  
  34.     if (connect(socketDESC, (struct sockaddr *) &serverADDRESS, sizeof(serverADDRESS)) < 0) {
  35.         printf("Erreur de connection\n");
  36.         return 1;
  37.     }
  38.  
  39.  
  40.     file_to_send = fopen (lfile,"r");
  41.     if(!file_to_send) {
  42.         printf("Erreur de lecture du fichier\n");
  43.         close(socketDESC);
  44.         return 0;
  45.     } else {
  46.     long fileSIZE;
  47.     fseek (file_to_send, 0, SEEK_END); fileSIZE =ftell (file_to_send);
  48.     rewind(file_to_send);
  49.  
  50.     sprintf(remoteFILE,"FBEGIN:%s:%i\r\n", rfile, fileSIZE);
  51.     send(socketDESC, remoteFILE, sizeof(remoteFILE), 0);
  52.  
  53.     percent = fileSIZE / 100;
  54.     while((ch=getc(file_to_send))!=EOF){
  55.         toSEND[0] = ch;
  56.         send(socketDESC, toSEND, 1, 0);
  57.         if( count1 == count2 ) {
  58.             printf("33[0;0H");
  59.             printf( "\33[2J");
  60.             printf("Nom du fichier: %s\n", lfile);
  61.             printf("Taille du fichier: %i Kb\n", fileSIZE / 1024);
  62.             printf("Poucentage : %d%% ( %d Kb)\n",count1 / percent ,count1 / 1024);
  63.             count1+=percent;
  64.         }
  65.         count2++;
  66.  
  67.     }
  68.     }
  69.     fclose(file_to_send);
  70.     close(socketDESC);
  71. return 0;
  72. }
  73.  
  74. int main(int argc, char* argv[])
  75. {
  76.     fileSEND("127.0.0.1", 31338, argv[1], argv[2]);
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement