在写ASP.NET AJAX那个Sys.Net.WebRequest的例子的时候,突然想起,何不用ACE实现一个简单的客户端,也发一个请求,看看返回的信息,是怎么样的,反正也在业余学习ACE,权当练习。
说动手就动手,三下五除二,代码如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
示例代码
1
#include "ace/INET_Addr.h"
2
#include "ace/SOCK_Stream.h"
3
#include "ace/SOCK_Connector.h"
4
#include "ace/Log_Msg.h"
5![](/Images/OutliningIndicators/None.gif)
6
int ACE_TMAIN(int,ACE_TCHAR* [])
7![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
8
ACE_INET_Addr server(2163,ACE_LOCALHOST);
9![](/Images/OutliningIndicators/InBlock.gif)
10
ACE_SOCK_Connector connector;
11
ACE_SOCK_Stream peer;
12![](/Images/OutliningIndicators/InBlock.gif)
13
if(connector.connect(peer,server) == -1)
14
ACE_ERROR_RETURN((LM_ERROR,
15
ACE_TEXT("%p\n"),
16
ACE_TEXT("connect")),1);
17![](/Images/OutliningIndicators/InBlock.gif)
18
char buf[1024];
19![](/Images/OutliningIndicators/InBlock.gif)
20
iovec iov[3];
21![](/Images/OutliningIndicators/InBlock.gif)
22
iov[0].iov_base = "GET ";
23
iov[0].iov_len = 4;
24![](/Images/OutliningIndicators/InBlock.gif)
25
iov[1].iov_base = "/Chapter2/GetTarget.aspx?Name=DonLiang ";
26
iov[1].iov_len = 39;
27![](/Images/OutliningIndicators/InBlock.gif)
28
iov[2].iov_base = "HTTP/1.1\r\n\r\n";
29
iov[2].iov_len = 13;
30![](/Images/OutliningIndicators/InBlock.gif)
31
if(peer.sendv_n(iov,3) == 1)
32
return (1);
33![](/Images/OutliningIndicators/InBlock.gif)
34
for(ssize_t n; ( n = peer.recv(buf,sizeof(buf)))>0;)
35
ACE::write_n(ACE_STDOUT,buf,n);
36![](/Images/OutliningIndicators/InBlock.gif)
37
peer.close();
38
39
write(1,"\n",1);
40
return(0);
41
}
这里也没什么特别的地方,也没什么难点,无非就是设置服务器的IP地址,连接服务器,发送请求,得到响应的信息,最后显示出来。
如图(和IE里的结果对比):