Voila le code d'un petit client html que j'ai trouve sur cppfrance:
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
SOCKET s;
in_addr addr;
sockaddr_in sin;
hostent * host;
WSADATA wsa;
char buf[1024];
int octet = 0;
int main(void)
{
WSAStartup(0x0202, &wsa);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(s == SOCKET_ERROR){
printf("socket : %d\n", WSAGetLastError());
}
host = gethostbyname("www.google.fr"); //l'adresse du site
memcpy(&addr, host->h_addr, host->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(80); // HTTP
sin.sin_addr = addr;
if(SOCKET_ERROR == connect(s,(sockaddr*)&sin, sizeof(sin))){
printf("connect : %d\n", WSAGetLastError());
}
strcpy(buf, "GET http://www.google.fr HTTP/1.0\n\n"); // la requete http
send(s, buf, strlen(buf), 0);
octet = recv(s, buf, sizeof(buf), 0);
for(int i=0; i<octet; i++)
printf("%c", buf[i]);
closesocket(s);
WSACleanup();
return 0;
}
Alors il marche tres bien sauf que lorsque je veux acceder a un site perso avec (au hasard http://toto.chez.tiscali.fr) je me retrouve avec une page d'erreur alors que le site est accessible sous internet explorer.
PK? :p
Comment on peut faire?