Bonjour,
J'ai un petit souci et j'aurais besoin d'un regard extérieur pour trouvez mon erreur,
Je me suis lancé dans la programmation (c++) réseaux
J'ai commencé par codé une classe gèrent les Sockets, puis j'ai crée une classe gèrent le protocole IRC et la je me lance dans une classe gèrent le protocole HTTP aparament le serveur reconnais correctement la requête mais je n'arrive a récupéré la page a proprement parlé j’obtiens :
HTTP/1.1 200 OK
Date: Fri, 02 Nov 2012 07:38:38 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: NID=65=Sv1539rRDdfH-y4HOeOyd8l7oLZMJvt_2lm1aM6nAEN1Pm1PzHW-dICfdvBsW
QyE07b-J-dNw9uggh2pGUxkNfUrvGO2VPBCwNHW-MwGoclECJ3w7drY3ApMznNZMDLG; expires=Sat
, 04-May-2013 07:38:38 GMT; path=/; domain=.; HttpOnly
P3P: CP="This is not a P3P policy! See
http://www.google.com/support/accounts/bi
n/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
voila le code :
classe socket
mysock.h
Code :
#ifndef DEF_MYSOCK
#define DEF_MYSOCK
#include <iostream>
#include <String>
typedef int socklen_t;
#include <winsock2.h>
//#pragma comment(lib,"Ws2_32.lib")
class mysock {
public:
mysock(const char* dest, int port);
virtual ~mysock();
std::string communication(std::string donne);
void senddonne(std::string donne);
std::string ecoute();
protected:
int initialisation();
int opensock();
int etablisement();
int closesession();
int liberationsock();
int liberationwinsowk();
int recevoirdonne();
int envoiedonne(std::string donne);
private:
int m_port;
const char* m_dest;
char buffer[65535];
std::string* retdonne;
WSADATA WSAData;
SOCKET sock;
SOCKADDR_IN sin;
};
#endif
mysock.cpp
Code :
#include "mysock.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
mysock::mysock(const char* dest, int port)
{
m_dest = dest;
m_port = port;
try
{
if(initialisation() != 0)
{
throw std::string("Erreur Initialisation !\n");
}
sock = opensock();
if(sock == INVALID_SOCKET)
{
throw std::string("Socket invalid");
}
if(etablisement() == SOCKET_ERROR)
{
throw std::string("Impossible de se connecté");
}
}
catch(std::string const& chaine)
{
std::cout << chaine << std::endl;
}
}
mysock::~mysock()
{
try
{
if(closesession() != 0)
{
throw std::string("Impossible de fermer la socket");
}
if(liberationsock() != 0)
{
throw std::string("Impossible de liberer la socket !\n");
}
if(liberationwinsowk() != 0)
{
throw std::string("Impossible de liberer winsock !\n");
}
}
catch(std::string const& chaine)
{
std::cout << chaine << std::endl;
}
}
std::string mysock::communication(std::string donne)
{
try
{
if(envoiedonne(donne) == SOCKET_ERROR)
{
throw std::string("Erreur envoye donne !\n");
}
strcpy(buffer, "" );
if(recevoirdonne() == SOCKET_ERROR)
{
throw std::string("Erreur reception donne !\n");
}
}
catch(std::string const& chaine)
{
std::cout << chaine << std::endl;
}
return buffer;
}
std::string mysock::ecoute()
{
try
{
strcpy(buffer, "" );
if(mysock::recevoirdonne() == SOCKET_ERROR)
{
throw std::string("Erreur reception donne !\n");
}
}
catch(std::string const& chaine)
{
std::cout << chaine << std::endl;
}
return buffer;
}
void mysock::senddonne(std::string donne)
{
try
{
if(mysock::envoiedonne(donne) == SOCKET_ERROR)
{
throw std::string("Erreur reception donne !\n");
}
}
catch(std::string const& chaine)
{
std::cout << chaine << std::endl;
}
donne.clear();
}
int mysock::initialisation()
{
return WSAStartup(MAKEWORD(2,2), &WSAData);
}
int mysock::opensock()
{
return socket(AF_INET, SOCK_STREAM, 0);
}
int mysock::etablisement()
{
sin.sin_addr.s_addr = inet_addr(m_dest);
sin.sin_family = AF_INET;
sin.sin_port = htons(m_port);
return connect(sock, (SOCKADDR*)&sin, sizeof(sin));
}
int mysock::closesession()
{
return shutdown(sock,2);
}
int mysock::liberationsock()
{
return closesocket(sock);
}
int mysock::liberationwinsowk()
{
return WSACleanup();
}
int mysock::recevoirdonne()
{
memset(buffer,0,sizeof(buffer));
return recv(sock, buffer, sizeof(buffer), 0);
}
int mysock::envoiedonne(std::string donne)
{
return send(sock,donne.c_str(), donne.size(),0);
}
classe http
http.h
Code :
#ifndef DEF_HTTP
#define DEF_HTTP
#include "mysock.h"
class http {
public:
http(const char* url);
virtual ~http();
std::string get(const char* page);
protected:
std::string header(const char* head_page, int en_tete);
std::string host();
std::string user_agent();
std::string Accept();
std::string Accept_language();
std::string Accept_encoding();
private:
int http_port;
std::string _url;
std::string _host;
std::string cr;
std::string lf;
mysock::mysock *chausette;
};
#endif
http.cpp
Code :
#include "http.h"
http::http(const char* url) : http_port(80), _url(url)
{
cr = 0x0d;
lf = 0x0a;
chausette = new mysock(url, http_port);
}
http::~http()
{
delete chausette;
}
std::string http::get(const char* page)
{
/* chausette->senddonne(http::header(page,0));
std::string buf, rep;
do
{
buf.clear();
buf = chausette->ecoute();
rep += buf;
}while(buf != "");
return rep;
*/
return chausette->communication(http::header(page,0));
}
std::string http::header(const char* head_page, int en_tete)
{
std::string requete;
if(en_tete == 0)
{
requete += "GET ";
requete += head_page;
requete += " HTTP/1.1";
requete += cr + lf;
}
requete += host();
requete += user_agent();
requete += Accept();
requete += Accept_language();
requete += Accept_encoding();
return requete + cr + lf;
}
std::string http::host()
{
return "Host: " + _url + cr + lf;
}
std::string http::user_agent()
{
return "User-Agent: Ox Agent" + cr + lf;
}
std::string http::Accept()
{
return "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + cr + lf;
}
std::string http::Accept_language()
{
return "Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3" + cr + lf;
}
std::string http::Accept_encoding()
{
return "Accept-Encoding: gzip, deflate" + cr + lf;
}
le main
Code :
#include <iostream>
#include "http.h"
using namespace std;
int main()
{
http tar("173.194.35.183");
std::string tmp = tar.get("/index.html");
cout << tmp;
return 0;
}
Peux être que quelqu'un vas voir l’erreur énorme qui se cache soit dans le code ou dans l'interprétation du protocol.
Merci d'avance.
Oxame