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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined (WIN32)
#include <winsock2.h>
typedef int socklen_t;
#else
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <arpa/inet.h>
#endif
#define MAX_MESS 100000
#define NOMBRE_DE_PAGES_A_TELECHARGER 5
int bor_connect_in(int soc,struct sockaddr_in *adr)
{
return connect(soc,(struct sockaddr *)adr,sizeof(struct sockaddr_in));
}
int main( int argc, char ** argv )
{
int sock,r,pos,i;
struct hostent *hp;
struct sockaddr_in adr;
char pagehtml[MAX_MESS];
#if defined (WIN32)
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
printf("\nWSAStartup failed\n");
#endif
hp = gethostbyname("heure.com");
if(hp == NULL)
{errno = WSAGetLastError();
printf("\ngethostbyname errno : %i %s\n",errno,strerror(errno));}
adr.sin_family = AF_INET;
adr.sin_port = htons(80);
memcpy(&adr.sin_addr.s_addr,hp->h_addr,hp->h_length);
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{errno = WSAGetLastError();
printf("\nsocket errno : %i %s\n",errno,strerror(errno));}
if( bor_connect_in(sock,&adr) < 0)
{errno = WSAGetLastError();
printf("\nConnect errno : %i %s\n",errno,strerror(errno));}
for(i = 0;i < NOMBRE_DE_PAGES_A_TELECHARGER; i++)
{
r = send(sock,"GET /index.html HTTP/1.0\n\n",strlen("GET /index.html HTTP/1.0\n\n"),0);
if(r < 0)
{errno = WSAGetLastError();
printf("\nSend errno : %i %s\n",errno,strerror(errno));}
pos=0;
while(1)
{
r = recv(sock,pagehtml+pos,MAX_MESS-pos-1,0);
if(r == 0 && pos == 0)
{printf("\nAucune réponses du serveur\n");break;}
else if(r == 0)
{printf("\nRéponse du serveur terminé, page html obtenu\nEntete http et pagehtml :\n\n %s\n\n",pagehtml);break;}
else if(r < 0)
{errno = WSAGetLastError();
printf("\nRecv errno : %i %s\n",errno,strerror(errno));break;}
pos+=r;
if(pos>=MAX_MESS-2)
{printf("\nBuffer plein\n");break;}
}
}
#if defined (WIN32)
WSACleanup ();
#endif
if(close(sock) < 0)
printf("\nClose errno : %i %s\n",errno,strerror(errno));
return EXIT_SUCCESS;
} |
Partager