1

私はソケットを試していて、非常に単純な webbot を構築しようとしています。

これは私のコードです:

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <cstring>
#include <iostream>
#include <string>

#define HTTP_PORT "80"

#define HOST "www.taringa.net"
#define PORT HTTP_PORT

#define IN_BUFFSIZE 1024
#define OUT_BUFFSIZE 1024

#define REQUEST "GET /Taringa/posts HTTP/1.0\r\nHost: www.taringa.net\r\nUser-Agent: foo\r\n\r\n"

using namespace std;

int main(int argc, char **argv) {

  struct addrinfo hints, *res;
  struct sockaddr_in servAddress;
  int sockfd;

  char addrstr[100];
  char buff_msg_out[OUT_BUFFSIZE], buff_msg_in[IN_BUFFSIZE];

  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ( getaddrinfo(HOST, HTTP_PORT, &hints, &res) != 0) {
     cerr << "Error en getaddrinfo" << endl;
     return -1;
  }


  // Crear socket
  if ( ( sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol) ) < 0 )
  {
   cerr << "Error en socket()" << endl;
   return -1;
  }


  // Iniciar conexion
  if ( connect(sockfd, res->ai_addr, res->ai_addrlen) == -1 )
  {
   cerr << "Error en connect()" << endl;
   cerr << "Error: " << strerror(errno) << endl;
   return -1;
  }

  cout << "Conectado con éxito" << endl;

  // Enviar datos
  strncpy(buff_msg_out, REQUEST, strlen(REQUEST));

  if ( send(sockfd, buff_msg_out, strlen(buff_msg_out), 0) <= 0 )
  {
   cerr << "Error en write()" << endl;
   return -1;
  }

  cout << "Mensaje enviado:" << endl << buff_msg_out << endl << endl;

  int bytes_recv = 0;

  while ( ( bytes_recv = recv(sockfd, buff_msg_in, IN_BUFFSIZE-1, 0)) > 0 )
  {
    buff_msg_in[bytes_recv] = '\0';
    cout << buff_msg_in << endl;        
  }

  freeaddrinfo(res);
  close(sockfd);

  return 0;
}

これは、リクエストが HTTP/1.0 の場合の出力です。

HTTP/1.1 200 OK
Server: n0
Date: Fri, 01 Feb 2013 19:57:26 GMT
Content-Type: text/html; charset=utf8
Connection: close
Set-Cookie: trngssn=06359673; path=/
Set-Cookie: trngssn=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: taringa_user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: lastNick=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: fbs=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: tws=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: iB-friendfind=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es" >
        <head profile="http://purl.org/NET/erdf/profile" prefix="og: http://ogp.me/ns
# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv=”X-Frame-Options” content=”Deny” />

                <link rel="alternate" type="application/atom+xml" title="Últimos Posts de Taringa" href="/rss/Taringa/posts/" />
                <link rel="alternate" type="application/atom+xml" title="Últimos Temas de Taringa" href="/rss/Taringa/tem
as/" />

                <title>Posts de Taringa!  - Taringa!</title>

...

    </body>
</html>

しかし、HTTP/1.1 を指定すると、応答は

HTTP/1.1 200 OK
Server: n0
Date: Fri, 01 Feb 2013 20:03:54 GMT
Content-Type: text/html; charset=utf8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: trngssn=81047255; path=/
Set-Cookie: trngssn=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: taringa_user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: lastNick=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: fbs=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: tws=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: iB-friendfind=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net

d0f
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es" >
        <head profile="http://purl.org/NET/erdf
/profile" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv=”X-Frame-Options” content=”Deny” />

                <link rel="alternate" type="application/atom+xml" title="Últimos Posts de Taringa" href="/rss/Taringa/posts/" />
                <link rel="alternate" type="application/atom+xml" title="Últimos Te
mas de Taringa" href="/rss/Taringa/temas/" />

                <title>Posts de Taringa!  - Taringa!</title>

...

ここに問題があります

    </body>
</html>

0

数秒待ってから通信を閉じます。

stackoverflow.com/about で試してみたところ、問題なく動作しました。サーバーがウェブページの後に私に送信する次のテキストを除いて

</html>HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>

何か不足していますか?

4

2 に答える 2

5

サーバーはチャンクエンコーディングを使用しています。d0fは、16進数で表されるオクテット単位の「チャンク」の長さです。0は次のチャンクの長さです(つまり、何もありません)。

于 2013-02-01T20:32:20.423 に答える
3

サーバー:
n0日付:2013年2月1日金曜日20:03:54 GMT
コンテンツタイプ:text / html; charset = utf8
Transfer-Encoding:チャンク
接続:keep-alive

サポートしていない場合は、HTTP1.1をサポートしているとは言わないでください。

すべてのHTTP/1.1アプリケーションは、「チャンク」転送コーディングを受信およびデコードできる必要があり、理解できないチャンク拡張拡張を無視する必要があります。--HTTP 1.1

于 2013-02-01T20:30:57.463 に答える