Use ace to capture data from Web Servers

Source: Internet
Author: User

I have already mentioned the configuration method for writing ace code in vs2008. The following code uses ace to retrieve webpage data from Baidu's Web server.

 

# 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 conne;
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.