Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: sIRC.c
- * Author: Cedrik Fuoco
- *
- * Created on 9 mai 2010, 21:50
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <winsock2.h>
- #include <string.h>
- #include <math.h>
- #define RECV_MAX 11393
- #define TAILLE 11392
- //---------------------------------------------------
- //Structures.
- //---------------------------------------------------
- //---------------------------------------------------
- //Autres fonctions.
- //---------------------------------------------------
- int ObtenirContentLength(const char *chaine);
- //---------------------------------------------------
- //Fonction principale.
- //---------------------------------------------------
- int main(int argc, char** argv)
- {
- WSADATA WSAData;
- SOCKET sock;
- SOCKADDR_IN sin;
- FILE* fichier = NULL;
- char buffer[TAILLE];
- char *sContenuBuffer;
- char sHeader[TAILLE];
- char *pContenuHeader;
- int iContent_Length;
- int iFileSize = 0;
- int iTailleBuffer = 0;
- int iTemp = 0;
- int iTailleHeader = 0;
- //---------------------------------------------------
- //Préparer les informations à envoyer au serveur.
- //---------------------------------------------------
- //char sHost[] = "Host: jeux-online.goldzoneweb.info\r\n";
- char sHost[] = "Host: i45.tinypic.com\r\n";
- char sUserAgent[] = "User-Agent: banane\r\n\r\n";
- //char sDonnee[300] = "GET /2i9pjl0.jpg HTTP/1.1\r\n"; //dark_vader
- //char sDonnee[300] = "GET /2vww7bm.jpg HTTP/1.1\r\n"; //feu
- char sDonnee[300] = "GET /2retdso.jpg HTTP/1.1\r\n"; //pwned
- //char sDonnee[300] = "GET /5ahyqv.jpg HTTP/1.1\r\n"; //fox
- strcat(sDonnee, sHost);
- strcat(sDonnee, sUserAgent);
- //---------------------------------------------------
- //Initialiser la connexion.
- //---------------------------------------------------
- WSAStartup(MAKEWORD(2,0), &WSAData);
- sock = socket(AF_INET, SOCK_STREAM, 0);
- //sin.sin_addr.s_addr = inet_addr("82.232.51.168");
- sin.sin_addr.s_addr = inet_addr("209.84.20.126");
- sin.sin_family = AF_INET;
- sin.sin_port = htons(80);
- //Si le client arrive à se connecté
- if(connect(sock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
- {
- send(sock, sDonnee, sizeof(sDonnee), 0); //Envoyer les informations pour la connexion.
- //Sauvgarder toute l'information reçu.
- iTailleBuffer = recv(sock, buffer, sizeof(buffer), 0);
- buffer[TAILLE] = '\0';
- printf("Premier Paquet: %i", iTailleBuffer);
- strcpy(sHeader, buffer);
- //---------------------------------------------------
- // Pointer sur le début du contenu à sauvegarder
- // (ignorer l'header)
- //---------------------------------------------------
- sContenuBuffer = strstr(buffer, "\r\n\r\n");
- if (sContenuBuffer != NULL)
- sContenuBuffer += 4;
- //---------------------------------------------------
- // Obtenir la taille du header.
- //---------------------------------------------------
- pContenuHeader = strstr(sHeader, "\r\n\r\n");
- if (pContenuHeader != NULL)
- pContenuHeader += 4;
- iTailleHeader = pContenuHeader-sHeader;
- printf("\nHeader: %i", iTailleHeader);
- //---------------------------------------------------
- // Obtenir la taille du content-length.
- //---------------------------------------------------
- iContent_Length = ObtenirContentLength(buffer); //Obtenir le Content-Length. [ObtenirContentLength]
- printf("\nContent-Length: %i", iContent_Length);
- iTailleBuffer -= iTailleHeader; //Enlever le taille du header.
- printf("\nTaille Buffer sans header: %i", iTailleBuffer);
- fichier = fopen("feu.jpg", "wb"); //Créer le fichier - Ouverture en mode écriture seulement.
- while (iTailleBuffer <= iContent_Length && fichier != NULL)
- {
- while (iFileSize < iTailleBuffer && fichier != NULL)
- {
- fputc(*sContenuBuffer, fichier);
- iFileSize += sizeof(*sContenuBuffer);
- sContenuBuffer++;
- }
- printf("\niFileSize: %i", iFileSize);
- sContenuBuffer -= sizeof(buffer) / sizeof(char);
- iTemp = recv(sock, buffer, sizeof(buffer), 0);
- iTailleBuffer += iTemp;
- if ( iTemp > 0 )
- {
- printf("\nOctets recu: %d", iTemp);
- }
- else if ( iTemp == 0 )
- printf("\nConnection closed");
- else
- printf("\nrecv failed: %d", WSAGetLastError());
- }
- }
- else
- printf("Impossible de se connecter\n");
- closesocket(sock); //Fermer le socket.
- WSACleanup(); //
- fclose(fichier); //Fermer le fichier.
- return (EXIT_SUCCESS);
- }
- /*
- Aller chercher la taille du fichier à télécharger. (Content-Length)
- */
- int ObtenirContentLength(const char *chaine)
- {
- char *sContentLength;
- char *sTemporaire;
- sContentLength = strstr(chaine, "Content-Length:");
- if (sContentLength != NULL)
- {
- sContentLength += 16;
- sTemporaire = sContentLength;
- for( ; *sContentLength != '\r' ; sContentLength++);
- *sContentLength = '\0';
- }
- return atoi(sTemporaire);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement