IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API, COM et SDKs Delphi Discussion :

WinSock :: Recv ne reçoit rien


Sujet :

API, COM et SDKs Delphi

  1. #1
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut WinSock :: Recv ne reçoit rien
    Bonjour,

    Je viens de commencer à coder avec les Windows Socket, et, comme prévu, j'ai déjà des problèmes: aucune erreur mais je ne reçois rien:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    procedure TForm1.Button1Click(Sender: TObject);
    var sock: Tsocket;
        inaddr: TInAddr;
        in_addr: TSockAddrIn;
        wsdata: TWSAData;
        host: PHostEnt;
        BufferOUT: string;
        BufferIN: array [0..2048] of char;
    begin
      Memo1.Lines.Clear;
      BufferOUT := '';
      BufferIN := '';
      WSAStartUp($202, wsdata);
      try
        host := GetHostByName(pchar(Edit1.Text));
        Move(host.h_addr, inaddr.S_addr, host.h_length);
        in_addr.sin_family := AF_INET;
        in_addr.sin_port := htons(StrToInt(Edit2.Text));
        in_addr.sin_addr := inaddr;
        Sock :=  socket(AF_INET, SOCK_STREAM, 0);
        IF connect(sock, in_addr, sizeof(in_addr)) <> Socket_Error then Memo1.Lines.Add('Connection Error: ' + Edit1.Text + ' on port ' + Edit2.Text + ' error nb: ' + IntToStr(WSAGetLastError));
        BufferOUT := 'GET http://' + Edit1.Text + Edit3.Text + ' HTTP/1.0' + chr(13) + chr(13) + chr(13);
        send(sock, BufferOUT, length(BufferOUT), 0);
        ZeroMemory(@BufferIN[0], SizeOf(BufferIn));
        recv(sock, BufferIN, sizeof(BufferIN), 0);
        Memo1.Lines.Add(BufferIN);
        CloseSocket(sock);
      finally
        WSACleanup;
      end;
        Memo1.Lines.Add('Done');
    end;
    Edit1: www.google.com
    Edit2: 80
    Edit3: /intl/fr/about.html

    Connect(), send() et recv() retournent -1

    J'ai essayé un string au lieu de l'array of char, mais c'est la même chose.

    Si je fais un "netstat 1", je ne vois pas google... il ne se connecte pas ? Pourtant, il n'y a pas d'erreur ...

    En gros, j'ai besoin de vos lumières
    Merci d'avance

    ------------------
    Edit:
    Je crois que ca vient de cette ligne:
    host := GetHostByName(pchar(Edit1.Text));
    ------------------

  2. #2
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    HTTP utilise du CR/LF il manque les chr(10)

    sinon j'ai un exemple ici
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  3. #3
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut
    Merci, je vais regarde ca

  4. #4
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut
    J'ai modifié mon code, maintenant, il se connecte correctement mais je ne reçois toujours rien :/

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    function TForm1.INetAddr(const Host:string):integer;
    var
     pHost:PChar;
     HostEnt:PHostEnt;
    begin
     if Host='' then begin // juste au cas ou ...
      Result:=INADDR_NONE;
     end else begin
      pHost:=PChar(Host);
      Result:=inet_addr(pHost); // est-ce une adresse au format x.y.z.w ?
      if Result=INADDR_NONE then begin
       HostEnt:=gethostbyname(pHost); // est-ce un nom d'hote ? (résolution DNS)
       if HostEnt<>nil then Result:=integer(pointer(HostEnt^.h_addr^)^);
      end;
     end;
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    var sock: Tsocket;
        inaddr: TInAddr;
        in_addr: TSockAddrIn;
        wsdata: TWSAData;
        BufferOUT, BufferIN: string;
        //BufferIN: array [0..2048] of char;
        i, j: integer;
    begin
      Memo1.Lines.Clear;
      BufferOUT := '';
      BufferIN := '';
      //ZeroMemory(@BufferIN[0], SizeOf(BufferIn));
      WSAStartUp($202, wsdata);
      try
        in_addr.sin_family := AF_INET;
        in_addr.sin_port := htons(StrToInt(Edit2.Text));
        in_addr.sin_addr.S_addr := INetAddr(Edit1.Text);
        if in_addr.sin_addr.S_addr=INADDR_NONE then Memo1.Lines.Add('No address specified');
        Sock :=  socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        IF connect(sock, in_addr, sizeof(in_addr)) = Socket_Error then Memo1.Lines.Add('Connection Error: ' + Edit1.Text + ' on port ' + Edit2.Text + ' error nb: ' + IntToStr(WSAGetLastError));
        BufferOUT := 'GET http://' + Edit1.Text + Edit3.Text + ' HTTP/1.0'#13#10;
        i := send(sock, BufferOUT, length(BufferOUT), 0);
        //sleep(500);
        SetLength(BufferIN, 1024);
        j := recv(sock, BufferIN, 1024, 0);
        Delete(BufferIN, pos(BufferIN, #13), 1);
        Delete(BufferIN, pos(BufferIN, #10), 1);
        Memo1.Lines.Add(BufferIN);
        CloseSocket(sock);
     
      finally
        WSACleanup;
      end;
        Memo1.Lines.Add('Done');
        Memo1.Lines.Add('Sent: ' + IntToStr(i));
        Memo1.Lines.Add('Received: ' + IntToStr(j));
    end;
    Memo:
    Done
    Sent: 55
    Received: 0 << déco, c'est bien ça ?

    Si je passe par netcat:
    nc www.google.com 80 <socket.dat >> reponse.txt

    Ou socket.dat contient:
    GET http://www.google.com/intl/fr/about.html HTTP/1.0

    J'ai une réponse:
    HTTP/1.0 200 OK
    Content-Type: text/html
    Last-Modified: Wed, 20 Aug 2008 21:14:31 GMT
    Set-Cookie: PREF=ID=c69655ba0465535f:TM=1219598435:LM=1219598435:S=TjDqA8dp03i83O-J; expires=Tue, 24-Aug-2010 17:20:35 GMT; path=/; domain=.google.com
    Date: Sun, 24 Aug 2008 17:20:35 GMT
    Server: gws
    Cache-Control: private, x-gzip-ok=""
    Connection: Close

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>&Agrave; propos de Google</title>
    <link rel="stylesheet" type="text/css" href="http://www.google.com/css/goog.css">
    <link rel="stylesheet" type="text/css" href="http://www.google.com/css/gooey.css">
    <link rel="stylesheet" type="text/css" href="http://www.google.com/css/layout.css">
    <style type="text/css">
    h1 {
    color:#636363;
    float:left;
    font-size:1.8em;
    padding:0;
    }

    h1 img {
    vertical-align:middle;
    }

    .header form {
    float:right;
    margin:1.6em 0 0;
    }

    .content h2 {
    color:#FFF;
    font-weight:bold;
    margin:0.2em 0;
    padding:0.2em 0.3em;
    }

    .content p {
    margin:0.5em 0;
    padding:0.2em 0.3em;
    }

    .content p a {
    font-size:1.2em;
    }

    .contentr1c1 h2 {
    background:#369;
    }

    .contentr1c2 h2 {
    background:#DDAD08;
    }

    .contentr2c1 h2 {
    background:#396;
    margin:0.7em 0 0;
    }

    .contentr2c2 h2 {
    background:#AA1002;
    margin:0.7em 0 0;
    }

    .promo {
    border:1px solid #000;
    margin:0.2em 0.7em 1em 0;
    padding:0.5em 0.3em;
    text-align:center;
    }

    .promo img {
    margin:0.3em 0.2em;
    }

    .promo-blue {
    border-color:#369;
    }

    .promo-yellow {
    border-color:#DDAD08;
    }

    .promo-green {
    border-color:#396;
    }

    .promo-red {
    border-color:#AA1002;
    }
    .contentr1 .g-unit, .contentr2 .g-unit {
    width:49.5% !important;
    }

    .leftnav {
    width:201px !important;
    }

    .content {
    margin:0 0 0 201px !important;
    }</style>


    <script type="text/javascript" language="javascript" src="https://ssl.google-analytics.com/urchin.js"></script>
    <script type="text/javascript" language="javascript">
    _uacct="UA-18000-1";
    _utcp="/intl/fr/";
    _uanchor=1;
    urchinTracker();
    </script>
    </head>
    <body>
    <div class="g-doc about">
    <div class="g-section header">
    <h1>&Agrave; propos de <a href="/"><img src="images/about_logo.gif" alt="" title="" width="175" height="65"></a>
    </h1>

    <form action="http://www.google.com/search" method="get">
    <input type="hidden" value="googleabout" name="output"><input type="hidden" value="http://www.google.com" name="sitesearch"><input type="hidden" value="fr" name="hl"><input type="text" value="" size="20" name="q">
    &nbsp;<input type="submit" value="Rechercher sur notre site" name="submit">
    </form>
    </div>
    <div class="g-section g-tpl-180">
    <div class="g-unit g-first leftnav">

    <div class="promo promo-blue">
    <strong><a href="http://toolbar.google.fr" onclick="javascript:urchinTracker('/embedmktg/toolbar');">Barre d'outils Google</a></strong>
    <br>
    <a href="http://toolbar.google.fr" onclick="javascript:urchinTracker('/embedmktg/toolbar');"><img src="http://www.google.com/images/icons/toolbar_about.gif" alt="Barre d'outils Google"></a>
    <br>Ajoutez un champ de recherche &agrave; votre navigateur.</div>

    <div class="promo promo-yellow">
    <strong><a href="http://maps.google.fr/">Google Maps</a></strong>
    <br>Plans, itin&eacute;raires et recherche de commerces.</div>

    <div class="promo promo-green">
    <strong><a href="https://adwords.google.fr/select/login?sourceid=awo&amp;subid=ww-fr-et-about&amp;medium=link" onclick="javascript:urchinTracker('/embedmktg/adwords');">Google AdWords</a></strong>
    <br>Augmentez vos clics, vos clients et votre chiffre d'affaires gr&acirc;ce &agrave; Google AdWords.</div>

    <div class="promo promo-red">
    <strong><a href="http://www.google.com/a/?utm_medium=et&amp;utm_source=about_page&amp;utm_campaign=fr&amp;token=app_fr" onclick="javascript:urchinTracker('/embedmktg/apps');">Google Apps</a></strong>
    <br>Am&eacute;liorez la communication au sein de votre entreprise gr&acirc;ce &agrave; Gmail et autres services associ&eacute;s.</div>


    </div>
    <div class="g-unit content">
    <div class="g-section g-tpl-50-50 contentr1">
    <div class="g-unit g-first contentr1c1">
    <h2>Nos produits</h2>
    <p>
    <a href="/support?hl=fr" onclick="javascript:urchinTracker('/site/help');">Aide</a>
    <br>Aide sur la recherche, les services et les produits Google...</p>
    <p>
    <a href="help/features.html" onclick="javascript:urchinTracker('/site/websearch');">Fonctions de recherche Google sur le Web</a>
    <br>Traduction, J'ai de la chance, informations mises en cache...</p>
    <p>
    <a href="options/" onclick="javascript:urchinTracker('/site/services');">Services et outils Google</a>
    <br>Barre d'outils, API Web Google, boutons...</p>
    <p>
    <a href="why_use.html" onclick="javascript:urchinTracker('/site/whygoogle');">Pourquoi utiliser Google&nbsp;?</a>
    <br>Informations compl&egrave;tes sur la recherche Google...</p>

    </div>
    <div class="g-unit contentr1c2">
    <h2>Pour les propri&eacute;taires de sites Web</h2>
    <p>
    <a href="ads/" onclick="javascript:urchinTracker('/site/ads');">Publicit&eacute;</a>
    <br>AdWords, AdSense...</p>
    <p>
    <a href="services/" onclick="javascript:urchinTracker('/site/solutions');">Solutions d'entreprise</a>
    <br>Google Search Appliance, Google Mini, recherche sur le Web...</p>
    <p>
    <a href="webmasters/" onclick="javascript:urchinTracker('/site/webmaster');">Centre pour les webmasters</a>
    <br>Service centralis&eacute; fournissant des informations compl&egrave;tes sur la mani&egrave;re dont Google explore et indexe les sites Web...</p>
    <p>
    <a href="submit_content.html" onclick="javascript:urchinTracker('/site/submit');">Envoyez votre contenu &agrave; Google</a>
    <br>Ajoutez votre site, Google Sitemaps...</p>

    </div>
    </div>
    <div class="g-section g-tpl-50-50 contentr2">
    <div class="g-unit g-first contentr2c1">
    <h2>La soci&eacute;t&eacute; Google</h2>
    <p>
    <a href="press/" onclick="javascript:urchinTracker('/site/press');">Centre de presse</a>
    <br>Infos, images, Zeitgeist...</p>
    <p>
    <a href="jobs/" onclick="javascript:urchinTracker('/site/jobs');">Carri&egrave;res chez Google</a>
    <br>Offres, avantages, culture...</p>
    <p>
    <a href="corporate/" onclick="javascript:urchinTracker('/site/corporate');">Informations sur la soci&eacute;t&eacute;</a>
    <br>Pr&eacute;sentation de l'entreprise, philosophie, diversit&eacute;, adresses...</p>

    </div>
    <div class="g-unit contentr2c2">
    <h2>Pour aller plus loin</h2>
    <p>
    <a href="contact/" onclick="javascript:urchinTracker('/site/contact');">Pour nous contacter</a>
    <br>FAQ, commentaires, lettre d'informations...</p>
    <p>
    <a href="stickers.html" onclick="javascript:urchinTracker('/site/logos');">Logos et photos</a>
    <br>Logos, Doodles et Googleurs en pleine action...</p>

    </div>
    </div>
    </div>
    </div>
    <div class="g-section footer">

    <p>
    &copy;2008 Google - <a href="privacy.html">R&egrave;gles de confidentialit&eacute;</a> - <a href="/accounts/TOS">Conditions d'utilisation</a>
    </p>
    </div>
    </div>
    </body>
    </html>


    Je ne comprend donc pas pourquoi dans mon programme, je ne reçois pas de réponse ...

    Une idée ?


    PS: j'ai aussi essayé avec ReadString & FillCache proposés dans le lien, j'ai le même résultat :/

  5. #5
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    ta requête déclare être du HTTP/1.0 hors elle n'est pas conforme à cette norme. Le site attends le reste de l'entête.
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  6. #6
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut
    Ok, je teste avec une entête plus complète.

    Edit:
    Mince, maintenant j'ai un Bad Request

    Memo:

    <html><head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>400 Bad Request</title>
    <style><!--
    body {font-family: arial,sans-serif}
    div.nav {margin-top: 1ex}
    div.nav A {font-size: 10pt; font-family: arial,sans-serif}
    span.nav {font-size: 10pt; font-family: arial,sans-serif; font-weight: bold}
    div.nav A,span.big {font-size: 12pt; color: #0000cc}
    div.nav A {font-size: 10pt; color: black}
    A.l:link {color: #6f6f6f}
    A.u:link {color: green}
    //--></style>
    <script><!--
    var rc=400;
    //-->
    </script>
    </head>
    <body text=#000000 bgcolor=#ffffff>
    <table border=0 cellpadding=2 cellspacing=0 width=100%><tr><td rowspan=3 width=1% nowrap>
    <b><font face=times color=#0039b6 size=10>G</font><font face=times color=#c41200 size=10>o</font><font face=times color=#f3c518 size=10>o</font><font face=times color=#0039b6 size=10>g</font><font face=times color=#30a72f size=10>l</font><font face=times color=#c41200 size=10>e</font>&nbsp;&nbsp;</b>
    <td>&nbsp;</td></tr>
    <tr><td bgcolor=#3366cc><font face=arial,sa
    Done
    Sent: 213
    Received: 1
    Requete:
    BufferOUT := 'GET http://' + Edit1.Text + Edit3.Text + ' HTTP/1.0'#13 +
    'Accept: */*'#13 +
    'Host: ' + Edit1.Text + #13 +
    'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13 +
    'Connection: Keep-Alive'#13 +
    #10;

  7. #7
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    il serait plus pertinent de nous donner le contenu de BufferOUT lors de l'envoie, car c'est lui qui est en cause
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  8. #8
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut
    Oui, désolé, je vais rassembler les pièces du puzzel:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
        BufferOUT := 'GET http://www.google.com/intl/fr/about.html HTTP/1.0'#13 +
                     'Accept: */*'#13 +
                     'Host:www.google.com' + #13 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13 +
                     'Connection: Keep-Alive'#13 +
                     'Content-Type: application/x-www-form-urlencoded; charset="utf-8"'#13#10#13
                     ;
    J'ai essayé pas mal de variantes mais je trouve pas ce qui cloche :/

  9. #9
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    il manque quelques #10, et pourquoi définir un content-type alors que la requête n'a pas de contenu ?!
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  10. #10
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        BufferOUT := 'GET http://www.google.com/intl/fr/about.html HTTP/1.0 '#13#10 +
                     'Accept: */* '#13#10 +
                     'Host: www.google.com'#13#10 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16 '#13#10 +
                     'Connection: Keep-Alive '#13#10
                     ;
        z:=length(BufferOut);
        i := send(sock, BufferOUT, z, 0);
    Toujours le même problème, j'ai lu la RFC et je trouve toujours pas de réponse :/
    Merci toutefois pour ton aide précieuse


    J'ai essayé en copier/collant mon HTTP-Header firefox:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
        BufferOUT := {'GET http://www.google.com/intl/fr/about.html HTTP/1.0'#13#10 +
                     'Host: www.google.com'#13#10 +
                     'Accept: text/plain'#13#10 +
                     'Accept-Language: en'#13#10 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
                     'Keep-Alive: 300'#13#10 +
                     'Connection: Keep-Alive'#13#10
                     ;}
     
                     'GET / HTTP/1.1'#13#10 +
                     'Host: www.google.com'#13#10 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
                     'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'#13#10 +
                     'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'#13#10 +
                     'Accept-Encoding: gzip,deflate'#13#10 +
                     'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'#13#10 +
                     'Keep-Alive: 300 '#13#10 +
                     'Connection: keep-alive'#13#10 +
                     'Cookie: PREF=ID=9d67be77ff4ba6ae:TM=1214847376:LM=1214847376:S=UEWn7F9t4WKLaZ0z; NID=14=JXX5RlGUB811OBkY0dqv96UndbWKmCe_OgG1AQ3MXg49QjTS5Tmdz6YlNq5jrzhuSo_IOY-zy9lms-DTC6C1Wz67CSgTiw-uzmdr-iWgsr58E0V0fjzGLYZLe-WRrxO8'#13#10
                     + #13#10;
    Avec ou sans la dernière ligne, le résultat est toujours le même: 400 Bad Request :/

    Code entier:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, winsock, StdCtrls;
     
    type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        Button1: TButton;
        Edit3: TEdit;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
        function INetAddr(const Host:string):integer;
      private
        { Private declarations }
      public
        { Public declarations }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    function TForm1.INetAddr(const Host:string):integer;
    var
     pHost:PChar;
     HostEnt:PHostEnt;
    begin
     if Host='' then begin // juste au cas ou ...
      Result:=INADDR_NONE;
     end else begin
      pHost:=PChar(Host);
      Result:=inet_addr(pHost); // est-ce une adresse au format x.y.z.w ?
      if Result=INADDR_NONE then begin
       HostEnt:=gethostbyname(pHost); // est-ce un nom d'hote ? (résolution DNS)
       if HostEnt<>nil then Result:=integer(pointer(HostEnt^.h_addr^)^);
      end;
     end;
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    var sock: Tsocket;
        inaddr: TInAddr;
        in_addr: TSockAddrIn;
        wsdata: TWSAData;
        BufferOUT, BufferIN, line: string;
        //BufferIN: array [0..2048] of char;
        i, j, z: integer;
    begin
      Memo1.Lines.Clear;
      BufferOUT := '';
      BufferIN := '';
      //ZeroMemory(@BufferIN[0], SizeOf(BufferIn));
      WSAStartUp($202, wsdata);
      try
        in_addr.sin_family := AF_INET;
        in_addr.sin_port := htons(StrToInt(Edit2.Text));
        in_addr.sin_addr.S_addr := INetAddr(Edit1.Text);
        if in_addr.sin_addr.S_addr=INADDR_NONE then Memo1.Lines.Add('No address specified');
        Sock :=  socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        IF connect(sock, in_addr, sizeof(in_addr)) = Socket_Error then Memo1.Lines.Add('Connection Error: ' + Edit1.Text + ' on port ' + Edit2.Text + ' error nb: ' + IntToStr(WSAGetLastError));
        BufferOUT := {'GET http://www.google.com/intl/fr/about.html HTTP/1.0'#13#10 +
                     'Host: www.google.com'#13#10 +
                     'Accept: text/plain'#13#10 +
                     'Accept-Language: en'#13#10 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
                     'Keep-Alive: 300'#13#10 +
                     'Connection: Keep-Alive'#13#10
                     ;}
     
                     'GET / HTTP/1.1'#13#10 +
                     'Host: www.google.com'#13#10 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
                     'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'#13#10 +
                     'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'#13#10 +
                     'Accept-Encoding: gzip,deflate'#13#10 +
                     'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'#13#10 +
                     'Keep-Alive: 300 '#13#10 +
                     'Connection: keep-alive'#13#10 +
                     'Cookie: PREF=ID=9d67be77ff4ba6ae:TM=1214847376:LM=1214847376:S=UEWn7F9t4WKLaZ0z; NID=14=JXX5RlGUB811OBkY0dqv96UndbWKmCe_OgG1AQ3MXg49QjTS5Tmdz6YlNq5jrzhuSo_IOY-zy9lms-DTC6C1Wz67CSgTiw-uzmdr-iWgsr58E0V0fjzGLYZLe-WRrxO8'#13#10
                     + #13#10;
     
        z:=length(BufferOut);
        i := send(sock, BufferOUT, z, 0);
        if i <= 1 then Memo1.Lines.Add('Oops');
     
     
        SetLength(BufferIN, 1024);
        For z := 1 to 1024 do
        begin
          j := recv(sock, BufferIN[z], 1, 0);
          if j <= 0 then begin Memo1.Lines.Add('Buggy'); break; end;
        end;
        Delete(BufferIN, pos(BufferIN, #13), 1);
        Delete(BufferIN, pos(BufferIN, #10), 1);
        Memo1.Lines.Add(BufferIN);
        CloseSocket(sock);
     
      finally
        WSACleanup;
      end;
        Memo1.Lines.Add('Done');
        Memo1.Lines.Add('Sent: ' + IntToStr(i));
        Memo1.Lines.Add('Received: ' + IntToStr(j));
    end;
     
    end.

  11. #11
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut
    J'ai sniffer mon programme, il y a un probleme lors de l'envoie de la requete:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    d$Ù Œõ }E …  ˜ó 	E Œõ ¼ø 	
    E Œõ ü•B ¬>Ù                                 ;Ù T$Ù WinSock 2.0                                                                                                                                                                                                                                                      Running                                                                                                                          ÿ»ÿ        P          d$Ù             d$Ù ˆÙ Ìö *C ¬>Ù –B ÷ —B ¬>Ù 
    C ÷ ÷ ¬>Ù 9   Øõ     Ðõ Ø™:~
    €  \ö ¢[:~
    €  , üÿÿÿ        äö 
    S:~¼:~
    Voila ce que le serveur reçoit ...


    Edit:
    Ok, j'ai réussi a envoyé une requete correcte:
    i := send(sock, pchar(BufferOut)^, z, 0);

    Je recois:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    GET / HTTP/1.1
    Host: www.google.com
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16
    Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300 
    Connection: keep-alive
    Cookie: PREF=ID=9d67be77ff4ba6ae:TM=1214847376:LM=1214847376:S=UEWn7F9t4WKLaZ0z; NID=14=JXX5RlGUB811OBkY0dqv96UndbWKmCe_OgG1AQ3MXg49QjTS5Tmdz6YlNq5jrzhuSo_IOY-zy9lms-DTC6C1Wz67CSgTiw-uzmdr-iWgsr58E0V0fjzGLYZLe-WRrxO8

    Et la réponse
    HTTP/1.0 200 OK
    Content-Type: text/html
    Last-Modified: Wed, 20 Aug 2008 21:14:31 GMT
    Set-Cookie: PREF=ID=0b5aafe39983cf54:TM=1219653336:LM=1219653336:S=__yqtyk9e1M6Z6qK; expires=Wed, 25-Aug-2010 08:35:36 GMT; path=/; domain=.google.com
    Date: Mon, 25 Aug 2008 08:35:36 GMT
    Server: gws
    Cache-Control: private, x-gzip-ok=""
    Connection: Close

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>&Agrave; propos de Google</title>
    <link rel="stylesheet" type="text/css" href="http://www.google.com/css/goog.css">
    <link rel="stylesheet" type="text/css" href="http://www.google.com/css/gooey.css">
    <link rel="stylesheet" type="text/css" href="http://www.google.com/css/layout.css">
    <style type="text/css">
    h1 {
    color:#636363;
    float:left;
    font-size:1.8em;
    padding:0;
    }

    h1 img {
    vertical-align:middle;
    }

    .header form {
    float:right;
    margin:1.6em 0 0;
    }

    .content h2
    Done
    Sent: 264
    Received: 1
    Ca fonctionne !

    Le code pour ceux qui veulent:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, winsock, StdCtrls;
     
    type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        Button1: TButton;
        Edit3: TEdit;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
        function INetAddr(const Host:string):integer;
        function SendData(socket:integer; data:pointer; datalen:integer):integer;
      private
        { Private declarations }
      public
        { Public declarations }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
    function TForm1.SendData(socket:integer; data:pointer; datalen:integer):integer;
    var
     p:pchar;
     l:integer;
    begin
     p:=data;
    // l:=datalen;
     result:=0;
     repeat
      l:=send(socket,p,datalen,0);
      if l<=0 then begin
       result:=l;
       exit;
      end;
      inc(result,l);
      inc(p,l);
      dec(datalen,l);
     until datalen=0;
    end;
     
    function TForm1.INetAddr(const Host:string):integer;
    var
     pHost:PChar;
     HostEnt:PHostEnt;
    begin
     if Host='' then begin // juste au cas ou ...
      Result:=INADDR_NONE;
     end else begin
      pHost:=PChar(Host);
      Result:=inet_addr(pHost); // est-ce une adresse au format x.y.z.w ?
      if Result=INADDR_NONE then begin
       HostEnt:=gethostbyname(pHost); // est-ce un nom d'hote ? (résolution DNS)
       if HostEnt<>nil then Result:=integer(pointer(HostEnt^.h_addr^)^);
      end;
     end;
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    var sock: Tsocket;
        inaddr: TInAddr;
        in_addr: TSockAddrIn;
        wsdata: TWSAData;
        BufferOUT, BufferIN, line: string;
        //BufferIN: array [0..2048] of char;
        i, j, z: integer;
        test: pointer;
    begin
      Memo1.Lines.Clear;
      BufferOUT := '';
      BufferIN := '';
      //ZeroMemory(@BufferIN[0], SizeOf(BufferIn));
      WSAStartUp($101, wsdata);
      try
        in_addr.sin_family := AF_INET;
        in_addr.sin_port := htons(StrToInt(Edit2.Text));
        in_addr.sin_addr.S_addr := INetAddr(Edit1.Text);
        if in_addr.sin_addr.S_addr=INADDR_NONE then Memo1.Lines.Add('No address specified');
        Sock :=  socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        IF connect(sock, in_addr, sizeof(in_addr)) = Socket_Error then Memo1.Lines.Add('Connection Error: ' + Edit1.Text + ' on port ' + Edit2.Text + ' error nb: ' + IntToStr(WSAGetLastError));
        BufferOUT := 'GET http://www.google.com/intl/fr/about.html HTTP/1.0'#13#10 +
                     'Host: www.google.com'#13#10 +
                     'Accept: text/plain'#13#10 +
                     'Accept-Language: en'#13#10 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
                     'Keep-Alive: 300'#13#10 +
                     'Connection: Keep-Alive'#13#10
                     + #13#10;
     
                     {'GET / HTTP/1.1'#13#10 +
                     'Host: www.google.com'#13#10 +
                     'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
                     'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'#13#10 +
                     'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'#13#10 +
                     'Accept-Encoding: gzip,deflate'#13#10 +
                     'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'#13#10 +
                     'Keep-Alive: 300 '#13#10 +
                     'Connection: keep-alive'#13#10 +
                     'Cookie: PREF=ID=9d67be77ff4ba6ae:TM=1214847376:LM=1214847376:S=UEWn7F9t4WKLaZ0z; NID=14=JXX5RlGUB811OBkY0dqv96UndbWKmCe_OgG1AQ3MXg49QjTS5Tmdz6YlNq5jrzhuSo_IOY-zy9lms-DTC6C1Wz67CSgTiw-uzmdr-iWgsr58E0V0fjzGLYZLe-WRrxO8'#13#10
                     + #13#10; }
     
        z:=length(BufferOut);
        test := @BufferOut[1];
        i := send(sock, pchar(BufferOut)^, z, 0);
        //i := SendData(sock, test, z);
        if i <= 1 then Memo1.Lines.Add('Oops');
     
        sleep(500);
        SetLength(BufferIN, 1024);
        //test := @BufferIN[1];
        For z := 1 to 1024 do
        begin
          j := recv(sock, BufferIN[z], 1, 0);
          if j <= 0 then begin Memo1.Lines.Add('Buggy'); end;
        end;
        Delete(BufferIN, pos(BufferIN, #13), 1);
        Delete(BufferIN, pos(BufferIN, #10), 1);
        Memo1.Lines.Add(BufferIN);
        CloseSocket(sock);
     
      finally
        WSACleanup;
      end;
        Memo1.Lines.Add('Done');
        Memo1.Lines.Add('Sent: ' + IntToStr(i));
        Memo1.Lines.Add('Received: ' + IntToStr(j));
    end;
     
    end.


    Grand merci a Paul_TOTH

  12. #12
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    ah oui j'étais passé à côté de ça...BufferOUT[1] fonctionne aussi parfaitement. Il faut juste se rappeler que les string sont des pointeurs.

    sinon tu devrais gérer les envoies partiels de données comme je le fais dans mon exemple car la fonction send() ne garantie pas que la totalité du buffer soit envoyée en une seule fois.
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  13. #13
    Membre confirmé
    Profil pro
    Directeur
    Inscrit en
    Juin 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Juin 2005
    Messages : 113
    Par défaut
    Ok, merci pour le conseil, j'essayerai de gerer ca correctement.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. recevfrom() ne reçoit rien
    Par merury dans le forum Réseau
    Réponses: 0
    Dernier message: 24/03/2012, 01h38
  2. Socket Client-Server ; Le client ne reçoit rien
    Par Ivelios dans le forum Entrée/Sortie
    Réponses: 3
    Dernier message: 08/01/2010, 17h00
  3. Probleme recv() [Winsock]
    Par kernox dans le forum Réseau
    Réponses: 3
    Dernier message: 11/04/2006, 20h58
  4. [MFC][WINSOCK] Problème avec fonction recv
    Par Le Farfadet dans le forum MFC
    Réponses: 4
    Dernier message: 23/09/2005, 11h00
  5. [winsock.h] Fonction recv() pour un socket en C
    Par Hikaru dans le forum Windows
    Réponses: 5
    Dernier message: 22/05/2004, 07h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo