Advertisement
Guest User

Untitled

a guest
May 28th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.86 KB | None | 0 0
  1. /*
  2.  * File:   sIRC.c
  3.  * Author: Cedrik Fuoco
  4.  *
  5.  * Created on 9 mai 2010, 21:50
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <winsock2.h>
  11. #include <string.h>
  12. #include <math.h>
  13.  
  14. #define RECV_MAX 11393
  15. #define TAILLE 11392
  16.                                                          //---------------------------------------------------
  17.                                                          //Structures.
  18.                                                          //---------------------------------------------------
  19.  
  20.                                                          //---------------------------------------------------
  21.                                                          //Autres fonctions.
  22.                                                          //---------------------------------------------------
  23. int ObtenirContentLength(const char *chaine);
  24.                                                          //---------------------------------------------------
  25.                                                          //Fonction principale.
  26.                                                          //---------------------------------------------------
  27. int main(int argc, char** argv)
  28. {
  29.     WSADATA WSAData;
  30.     SOCKET sock;
  31.     SOCKADDR_IN sin;
  32.  
  33.     FILE* fichier = NULL;
  34.  
  35.     char buffer[TAILLE];
  36.     char *sContenuBuffer;
  37.  
  38.     char sHeader[TAILLE];
  39.     char *pContenuHeader;
  40.  
  41.     int iContent_Length;
  42.     int iFileSize = 0;
  43.     int iTailleBuffer = 0;
  44.     int iTemp         = 0;
  45.     int iTailleHeader = 0;
  46.                                                          //---------------------------------------------------
  47.                                                          //Préparer les informations à envoyer au serveur.
  48.                                                          //---------------------------------------------------
  49.  
  50.     //char sHost[] = "Host: jeux-online.goldzoneweb.info\r\n";
  51.     char sHost[] = "Host: i45.tinypic.com\r\n";
  52.     char sUserAgent[] = "User-Agent: banane\r\n\r\n";
  53.     //char sDonnee[300] = "GET /2i9pjl0.jpg HTTP/1.1\r\n"; //dark_vader
  54.     //char sDonnee[300] = "GET /2vww7bm.jpg HTTP/1.1\r\n"; //feu
  55.     char sDonnee[300] = "GET /2retdso.jpg HTTP/1.1\r\n"; //pwned
  56.     //char sDonnee[300] = "GET /5ahyqv.jpg HTTP/1.1\r\n"; //fox
  57.  
  58.     strcat(sDonnee, sHost);
  59.     strcat(sDonnee, sUserAgent);
  60.  
  61.                                                          //---------------------------------------------------
  62.                                                          //Initialiser la connexion.
  63.                                                          //---------------------------------------------------
  64.     WSAStartup(MAKEWORD(2,0), &WSAData);
  65.  
  66.     sock = socket(AF_INET, SOCK_STREAM, 0);
  67.     //sin.sin_addr.s_addr = inet_addr("82.232.51.168");
  68.     sin.sin_addr.s_addr = inet_addr("209.84.20.126");
  69.     sin.sin_family = AF_INET;
  70.     sin.sin_port = htons(80);
  71.  
  72.                                                          //Si le client arrive à se connecté
  73.     if(connect(sock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
  74.     {
  75.         send(sock, sDonnee, sizeof(sDonnee), 0);         //Envoyer les informations pour la connexion.
  76.                                                          //Sauvgarder toute l'information reçu.
  77.         iTailleBuffer = recv(sock, buffer, sizeof(buffer), 0);
  78.         buffer[TAILLE] = '\0';
  79.         printf("Premier Paquet: %i", iTailleBuffer);
  80.         strcpy(sHeader, buffer);
  81.                                                          //---------------------------------------------------
  82.                                                          // Pointer sur le début du contenu à sauvegarder
  83.                                                          // (ignorer l'header)
  84.                                                          //---------------------------------------------------
  85.         sContenuBuffer = strstr(buffer, "\r\n\r\n");
  86.         if (sContenuBuffer != NULL)
  87.            sContenuBuffer += 4;
  88.  
  89.                                                          //---------------------------------------------------
  90.                                                          // Obtenir la taille du header.
  91.                                                          //---------------------------------------------------
  92.         pContenuHeader = strstr(sHeader, "\r\n\r\n");
  93.         if (pContenuHeader != NULL)
  94.            pContenuHeader += 4;
  95.         iTailleHeader = pContenuHeader-sHeader;
  96.         printf("\nHeader:           %i", iTailleHeader);
  97.  
  98.  
  99.                                                          //---------------------------------------------------
  100.                                                          // Obtenir la taille du content-length.
  101.                                                          //---------------------------------------------------
  102.         iContent_Length = ObtenirContentLength(buffer);  //Obtenir le Content-Length. [ObtenirContentLength]
  103.         printf("\nContent-Length: %i", iContent_Length);
  104.  
  105.  
  106.  
  107.         iTailleBuffer -= iTailleHeader;                  //Enlever le taille du header.
  108.         printf("\nTaille Buffer sans header: %i", iTailleBuffer);
  109.  
  110.         fichier = fopen("feu.jpg", "wb");         //Créer le fichier - Ouverture en mode écriture seulement.
  111.  
  112.         while (iTailleBuffer <= iContent_Length && fichier != NULL)
  113.         {
  114.             while (iFileSize < iTailleBuffer && fichier != NULL)
  115.             {
  116.                 fputc(*sContenuBuffer, fichier);
  117.                 iFileSize += sizeof(*sContenuBuffer);
  118.                 sContenuBuffer++;
  119.             }
  120.             printf("\niFileSize: %i", iFileSize);
  121.             sContenuBuffer -= sizeof(buffer) / sizeof(char);
  122.  
  123.             iTemp = recv(sock, buffer, sizeof(buffer), 0);
  124.             iTailleBuffer += iTemp;
  125.             if ( iTemp > 0 )
  126.             {
  127.                printf("\nOctets recu: %d", iTemp);
  128.             }
  129.             else if ( iTemp == 0 )
  130.                printf("\nConnection closed");
  131.             else
  132.                printf("\nrecv failed: %d", WSAGetLastError());
  133.         }
  134.     }
  135.     else
  136.       printf("Impossible de se connecter\n");
  137.  
  138.     closesocket(sock);                                   //Fermer le socket.
  139.     WSACleanup();                                        //
  140.     fclose(fichier);                                     //Fermer le fichier.
  141.  
  142.     return (EXIT_SUCCESS);
  143. }
  144.  
  145. /*
  146.    Aller chercher la taille du fichier à télécharger. (Content-Length)
  147. */
  148. int ObtenirContentLength(const char *chaine)
  149. {
  150.     char *sContentLength;
  151.     char *sTemporaire;
  152.  
  153.     sContentLength = strstr(chaine, "Content-Length:");
  154.     if (sContentLength != NULL)
  155.     {
  156.         sContentLength += 16;
  157.         sTemporaire = sContentLength;
  158.         for( ; *sContentLength != '\r' ; sContentLength++);
  159.         *sContentLength = '\0';
  160.     }
  161.     return atoi(sTemporaire);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement