• 用ACE从Web服务器抓数据


    前面已经说了VS2008写ACE代码的配置方法,下面这段代码是用ACE从百度的Web服务器取网页数据的代码

    #include "ace/INET_Addr.h"
    #include "ace/SOCK_Connector.h"
    #include "ace/SOCK_Stream.h"
    #include "ace/Time_Value.h"

     

    int main (int argc, char *argv[])
    {
     const char *pathname = "index.html";
     const char *server_hostname = "
    www.baidu.com";


     char *header[4];
     header[0] = "GET
    http://www.baidu.com HTTP/1.1/r/n";
     header[1] = "Host:
    www.baidu.com/r/n";
     header[2] = "Connection: Close/r/n";
     header[3] = "/r/n";

     

     ACE_SOCK_Connector connector;
     ACE_SOCK_Stream peer;
     ACE_INET_Addr peer_addr;

     if (peer_addr.set(80, server_hostname) == -1)
     {
      return 1;
     }


     if (connector.connect(peer, peer_addr) == -1)
     {
      return 1;
     }

     char buf[BUFSIZ];
     iovec iov[4];
     for (int i = 0; i < 4; i++)
     {
      iov[i].iov_base = header[i];
      iov[i].iov_len = strlen(header[i]);
     }

     // Wait no more than 5 seconds to send or receive data.
     ACE_Time_Value timeout(5);

     peer.sendv_n(iov, 4, &timeout);

     ssize_t lenRecv = peer.recv(buf, sizeof buf, &timeout);
     while (lenRecv > 0)
     {
      ACE::write_n(ACE_STDOUT, buf, lenRecv);
      lenRecv = peer.recv(buf, sizeof buf, &timeout);
     }

     int result_close = peer.close();

     system("pause");
     return result_close ? 1 : 0;
    }

  • 相关阅读:
    html之marquee详解
    CSS盒模型
    基于windows API的手柄/键盘映射编程(一)
    阿超的烦恼来临的始端
    阿超的小目标
    程序员的800字作文
    Link to Coding
    项目经理都干些什么啊
    停不下来的英语课联想
    Markdown
  • 原文地址:https://www.cnblogs.com/yjh4866/p/6253888.html
Copyright © 2020-2023  润新知