前面已经说了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;
}