初窥ACE

作者在 2006-10-25 16:47:00 发布以下内容

一个可移植性很高的Frame.

// AceSocket.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include "ace/Time_Value.h"
using namespace std;

int main(int argc, char* argv[])
{
 const char* pathname="/index.html";

/* struct SevHsName
 {
  char *sev_hsname;
 }SHN[5]={"ace.ece.uci.edu","www.sina.com","www.163.com","www.baidu.com","wfpb.bc-cn.net"};
*/ 

 const char* server_hostname="www.163.com";
 
 ACE_INET_Addr peer_addr;
 ACE_SOCK_Connector connector;
 ACE_SOCK_Stream peer;
 
 if(peer_addr.set(80,server_hostname) == -1)
 {
  std::cout<<"fail@!set"<<"\n";
  return 1;
 }
 else if(connector.connect(peer,peer_addr) == -1)
 {
  std::cout<<"fail@!connect"<<"\n";
  return 1;
 }
 
 char buf[BUFSIZ];
 iovec iov[3];
 iov[0].iov_base="GET ";
 iov[0].iov_len=sizeof("GET ");
 iov[1].iov_base=(char*)pathname;
 iov[1].iov_len=strlen(pathname);
 iov[2].iov_base=" HTTP/1.1 \r\n\r\n";
 iov[2].iov_len=sizeof(" HTTP/1.1 \r\n\r\n");
 
 ACE_Time_Value timeout(10);
 if(peer.sendv_n(iov,3/*,&timeout*/,&ACE_Time_Value::zero) == -1)
 {
  std::cout<<"fail@!send<<\n";
  return 1;
 }
 for(ssize_t n;(n=peer.recv(buf,sizeof(buf)))>5;)
  ACE::write_n(ACE_STDOUT,buf,sizeof(buf));
 
 std::cout<<"\n"<<"success@!"<<"\n";
 return peer.close()==-1?1:0;
}

C++语法 | 阅读 1873 次
文章评论,共0条
游客请输入验证码
浏览69248次