#include "ace/INET_Addr.h" #include "ace/SOCK_Connector.h" #include "ace/SOCK_Stream.h" #include "ace/Log_Msg.h" int ACE_TMAIN (int argc, ACE_TCHAR *argv[]){ const char *pathname=argc>1 ? argv[1]:"index.html"; const char *servername=argc>2 ? argv[2]:"ace.ece.uci.edu"; ACE_SOCK_Connector connector; ACE_SOCK_Stream peer; ACE_INET_Addr peer_addr; if(peer_addr.set(80,servername)==-1) return -1; else if(connector.connect(peer,peer_addr)==-1) return -1; char buf[BUFSIZ] = {0}; iovec iov[3]; iov[0].iov_base = "GET "; iov[0].iov_len = strlen("GET ") + 1; // 比实际大小多一个字节,不然会有400 Bad Request问题,不知道为什么 iov[1].iov_base = (char*)pathname; iov[1].iov_len = strlen(pathname); iov[2].iov_base = "HTTP/1.0\r\n\r\n"; iov[2].iov_len = 13; if(peer.sendv(iov, 3) == -1) return -1; for(ssize_t n; (n = peer.recv(buf, sizeof buf)) > 0; ) ACE::write_n(ACE_STDOUT ,buf ,n); system("pause"); return peer.close() == -1 ? 1 : 0; }