Beaucoup de code en ce moment sur le forum je vous propose aussi du code.
Cette classe vous permettras d'utiliser la méthode GET en http et vous permet aussi d'utiliser le réseau en mode TCP/IP facilement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | /* * Copyright (C) 2008 plutech */ #include "CNet.h" CNet* CNet::singleton = NULL; int CNet::connection(char* server_name,int port) { struct sockaddr_in serverSockAddr; struct hostent *serverHostEnt; memset(&serverSockAddr,0,sizeof(serverSockAddr)); serverHostEnt = gethostbyname(server_name); if (serverHostEnt == NULL) { printf("Unable to look up server : %s\n",server_name); return -1; } memcpy(&serverSockAddr.sin_addr,serverHostEnt->h_addr,serverHostEnt->h_length); serverSockAddr.sin_port = htons(port); serverSockAddr.sin_family = AF_INET; sock = socket(AF_INET,SOCK_STREAM,0); if (sock<0) { printf("Socket creation failed\n"); return -1; } if(connect(sock,(struct sockaddr *)&serverSockAddr,sizeof(serverSockAddr))<0) { printf("Connection to server failed\n"); return -1; } return 1; } void CNet::send_p(char* packet) { int n = send (sock, packet, sizeof (*packet), 0); } char* CNet::recv_p(SOCKET sock) { char text[1500]; //mtu max int n = recv (sock, text, sizeof text - 1, 0); if(n == -1) { closesocket(sock); return "crash"; } text[n] = 0; return text; } int CNet::poschaine(char *chaine1, char *chaine2, int depart){ unsigned int i,j; for( i = depart; i < strlen(chaine1); i++) { if(chaine1[i] == chaine2[0]) { int rate = 0; for(j = 0; j < strlen(chaine2); j++)if( chaine1[i+j] != chaine2[j]) rate=1; if(rate == 0)return i; } } return -1; } int CNet::hextoint2(char *hex) { if(strlen(hex)>0) { if(hex[0]>'0'&&hex[0]<='9') return hextoint2(hex+1)*16+hex[0]-'0'; else if(hex[0]>='a'&&hex[0]<='f') return hextoint2(hex+1)*16+hex[0]-'a'+ 10; else if(hex[0]>='A'&&hex[0]<='F') return hextoint2(hex+1)*16+hex[0]-'A'+ 10; else return hextoint2(hex+1)*16; } else return 0; } int CNet::hextoint(char *hex) { char *temp = (char*)malloc(strlen(hex)); unsigned int i; for(i=0;i<strlen(hex);i++) temp[i]=hex[strlen(hex)-i-1]; return hextoint2(temp); } int CNet::InitNetwork() { #if defined WIN32 WSADATA wsa; WSAStartup(MAKEWORD(2,0),&wsa); #endif return 0; } int CNet::CloseNetwork() { #if defined WIN32 WSACleanup(); #endif return 0; } CHttp::CHttp() { InitNetwork(); } CHttp::~CHttp() { CloseNetwork(); delete data; } char *CHttp::chunked_decode(char *buffer) { char *buf = (char*)malloc(256*256); unsigned int i=0,j,k=0; while(i<strlen(buffer)) { char hex[5]; unsigned int size; sscanf(buffer+i,"%s",hex); size = hextoint(hex); i+=strlen(hex)+2; for(j=0;j<size;j++) { buf[k] = buffer[i+j]; k++; } i+=size+2; } buf[k]='\0'; return buf; } void CHttp::read_commande(char *buffer) { if(poschaine(buffer,"HTTP/1.1",0)>-1) { int num; sscanf(buffer,"HTTP/1.1 %d",&num); status = num; } else if(poschaine(buffer,"Transfer-Encoding",0)>-1) { if(poschaine(buffer,"chunked",0)>-1) encoding = chunked; else encoding = normal; } } int CHttp::read_header(char *buffer) { unsigned int i=0,j; while(i<strlen(buffer)) { char *buf = (char*)malloc(256); for(j=0;buffer[i+j]!='\r'||buffer[i+j+1]!='\n';j++) buf[j] = buffer[i+j]; buf[j]='\0'; read_commande(buf); i+=j+2; if(strlen(buf)==0) break; } return i; } void CHttp::save(char *filename) { // Not needed for actual enginer } void CHttp::load(char* adress) { char* host; char str[1024]; strcpy(str,adress); char* file; if (strstr(adress, "http://") != NULL){ strtok(str,"/"); host = strtok(NULL,"/"); file = strtok(NULL," /"); } else{ host = strtok(str," /"); file = strtok(NULL," /"); } cout << host << endl; cout << file << endl; char commande[500]; char *buffer = (char*)malloc(256*256); char buf[BUFSIZE]; int lu; connection(host,80); sprintf(commande,"GET /%s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\n\r\n",file,host); send(sock,commande,strlen(commande)+1,0); int pos=0; do { lu=recv(sock,&(buf[0]),BUFSIZE,0); strncpy(&(buffer[pos]),&(buf[0]),lu); pos+=lu; } while(lu>0); data = new char[strlen(buffer)+1]; strcpy(data,buffer); shutdown(sock,2); closesocket(sock); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | /* * Copyright (C) 2008 plutech */ #ifndef _CNET_H_ #define _CNET_H_ #include <string> #if defined (WIN32) #include <winsock2.h> #elif defined (linux) #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close (s) typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; #endif #define BUFSIZE 1024 #define normal 0 #define chunked 1 class CNet{ public: // --------------------------------------------------------- // Network method // --------------------------------------------------------- void send_p(char* packet); char* recv_p(SOCKET sock); int InitNetwork(); int CloseNetwork(); int connection(char* adr, int port); int poschaine(char *chaine1, char *chaine2, int depart); int hextoint2(char *hex); int hextoint(char *hex); int status; int encoding; void CloseCNet() { delete log; } static CNet* GetSingleton() { if(singleton == NULL) { singleton = new CNet; singleton->InitNetwork(); } return singleton; } SOCKET sock; private: // --------------------------------------------------------- // Variable private in class // --------------------------------------------------------- CLogger* log; static CNet* singleton; }; class CHttp : public CNet{ public: CHttp(); ~CHttp(); int read_header(char *buffer); void read_commande(char *buffer); void load(char* adress); char *chunked_decode(char *buffer); void save(char *filename); char* GetData() { return data; } void FreeMemory() { delete data; } char* data; }; #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> using namespace std; int main(void) { CHttp *chttp = new CHttp; /// Chargment du fichier chttp->load("Unable to look up server : %s\n"); /// On affiche le ficher en récupérant les données cout << chttp->GetData() << endl; /// On ferme le réseau delete chttp; } |