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