andylin02 发表于 2013-1-28 19:24:55

ACE 入门

 
ACE 入门


一、ACE编译
a)         下载ACE源文件,你可以从官方网站下载,我的是ACE+TAO+CIAO-5.6。
下载地址:
http://download.dre.vanderbilt.edu/
注意我这里下载的是.zip作为扩展名的版本,这是适用于Windows 2000, XP (MSVC++ 7.1, and 8)。
b)        将你下载的ACE.zip解压缩,假设目录为:
E:\ACE_wrappers
在该目录下你可以发现ACE-INSTALL.html文件,这个文件介绍了如何编译ACE,如果你英文不错的话,可以参考该文档。
c)        设置环境变量:
ACE_ROOT:ACE_wrappers所在的目录,这里是E:\ACE_wrappers
PATH:%ACE_ROOT%\lib
设置ACE_ROOT是为了便于设置ACE有关的include头文件目录
设置PATH,是为了当你的基于ACE的应用程序执行时,系统可以找到相应的ACE动态连接库,发布版本对应的是ACE的发布版本DLL(ACE.dll),调试版本对应的是ACE的调试版本DLL(ACEd.dll)
d)        打开%ACE_ROOT%,找到ACE_vc8.sln,即ACE对应的VC8 for desktop/server的解决方案。
找到ACE项目,并且在头文件目录添加一个头文件config.h,文件内添加如下内容:
                         i.              (必选)包含ACE 在WIN32平台上的配置文件
#include "ace/config-win32.h"
                       ii.              (可选)如果你想使用标准C++头文件(标准 C++ 草案2中和MSVC一致的iostream, cstdio等,可移植、跨平台应用目的),在config.h文件中的所有#include语句之前添加下面一行:
#define ACE_HAS_STANDARD_CPP_LIBRARY 1
                      iii.              (可选)如果你不想展开ACE内联函数,在config.h文件中的所有#include语句之前添加下面一行:
#define ACE_NO_INLINE
这在当你选择将ACE编译为静态连接库(或者使用ACE)的静态连接库ACE.lib或者ACEd.lib时可以减小程序占用的空间。
如果你想将ACE编译为静态连接库,晴添加下面这行:
#define ACE_AS_STATIC_LIBS 1
某任情况下你将得到动态和静态两个连接库,有时添加了这个宏定义会导致Lib文件不能正常生成,这时可以把这个宏定义去掉.


                     iv.              (可选)如果你想使用带有MFC库的ACE,请添加如下一行:
#define ACE_HAS_MFC 1
默认情况下ACE使用MSVC动态运行库(因为任何NT平台都有)
如果你想使用MFC静态库,请添加下面一行:


建议使用默认。
                       v.              (可选)如果你想ACE使用 Unicode 字符集,请添加如下两行:
#define ACE_HAS_WCHAR
#define ACE_USE_WCHAR
默认ACE使用多字节字符集
注意,如果你设置了该项,那么在使用ACE的项目中,记得在"项目属性->配置属性->项目默认值->字符集"中配置为相应的使用 Unicode 字符集。如果使用默认,将使用的ACE项目配置为使用多字节字符集。
       下面是我的%ACE_ROOT%\ace\config.h内容
#define ACE_HAS_STANDARD_CPP_LIBRARY 1
//#define ACE_NO_INLINE
//#define ACE_AS_STATIC_LIBS 1
//#define ACE_HAS_WCHAR
//#define ACE_USE_WCHAR


#include "ace/config-win32.h"


e)         最后选择Debug或者Release生成ACE项目,构建完后,你会在%ACE_ROOT%\lib目录看到相应版本的ACE动态连接库和静态连接库:
ACEd.lib, ACEd.dll(Debug版本)或者ACE.lib ACE.dll(Release版本)
二、ACE测试
测试程序你可以在任何一本介绍ACE的教程中找到。
下面是在你出现问题时需要首先查看的内容:
a)         “工具”->“选项”->“项目和解决方案” ->“VC++目录”->“选项”,在右边的“包含文件”中添加
$(ACE_ROOT)
这是为了让编译器找到ACE相关的头文件。
在右边的“库文件”中添加
$(ACE_ROOT)\lib
这是为了让编译器构建项目时链接ACE
b)        保证你当前项目的配置(Debug还是Release)是否和你为项目设置的“附加依赖项”对应的ACE静态库对应(Debug对应ACEd.lib,Release对应ACE.lib)
为了避免出现此类问题,最好在你的程序开始,加上下面几行预处理指令:
 #ifdef _DEBUG
#pragma comment (lib,"ACEd.lib")
#else
#pragma comment (lib,"ACE.lib")
#endif


c)      最后给一个源码


这时ACE程序员教程中使用ACE_SOCK_Stream的服务器/客户端通信的代码,略加改动。
 
//ServerMain.cpp#ifdef _DEBUG#pragmacomment (lib,"aced.lib")#else#pragmacomment (lib,"ace.lib")#endif#include "ace/OS.h"#include "ace/Log_Msg.h"#include "ace/SOCK_Acceptor.h"#include "ace/SOCK_Stream.h"#define SIZE_DATA 18#define SIZE_BUF 1024#define NO_ITERATIONS 5class Server{public:    Server (int port): server_addr_(port),peer_acceptor_(server_addr_)    {      data_buf_= new char;    }    //Handle the connection once it has been established. Here the    //connection is handled by reading SIZE_DATA amount of data from the    //remote and then closing the connection stream down.    int handle_connection()    {      // Read data from client      for(int i=0;i<NO_ITERATIONS;i++)      {            int byte_count=0;            if( (byte_count=new_stream_.recv_n (data_buf_, SIZE_DATA, 0))==-1)                ACE_ERROR ((LM_ERROR, "%p ", "Error in recv"));            else            {                data_buf_=0;                ACE_DEBUG((LM_DEBUG,"Server received %s",data_buf_));            }      }      // Close new endpoint      if (new_stream_.close () == -1)            ACE_ERROR ((LM_ERROR, "%p ", "close"));      return 0;    }    //Use the acceptor component peer_acceptor_ to accept the connection    //into the underlying stream new_stream_. After the connection has been    //established call the handle_connection() method.    int accept_connections ()    {      if (peer_acceptor_.get_local_addr (server_addr_) == -1)            ACE_ERROR_RETURN ((LM_ERROR,"%p ","Error in get_local_addr"),1);      ACE_DEBUG ((LM_DEBUG,"Starting server at port %d ",            server_addr_.get_port_number ()));      // Performs the iterative server activities.      while(1)      {            ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);            if (peer_acceptor_.accept (new_stream_, &client_addr_, &timeout)== -1)            {                ACE_ERROR ((LM_ERROR, "%p ", "accept"));                continue;            }            else            {                ACE_DEBUG((LM_DEBUG,                  "Connection established with remote %s:%d ",                  client_addr_.get_host_name(),client_addr_.get_port_number()));                //Handle the connection                handle_connection();            }      }    }private:    char *data_buf_;    ACE_INET_Addr server_addr_;    ACE_INET_Addr client_addr_;    ACE_SOCK_Acceptor peer_acceptor_;    ACE_SOCK_Stream new_stream_;};int run_main (int argc, ACE_TCHAR *argv[]);intACE_TMAIN (int argc, ACE_TCHAR *argv[]){    return run_main (argc, argv);}int run_main (int argc, ACE_TCHAR *argv[]){    if(argc<2)    {      ACE_ERROR((LM_ERROR,"Usage %s <port_num>", argv));      ACE_OS::exit(1);    }    Server server(ACE_OS::atoi(argv));    server.accept_connections();    return 0;} //ClientMain.cpp#ifdef _DEBUG#pragmacomment (lib,"aced.lib")#else#pragmacomment (lib,"ace.lib")#endif#include "ace/OS.h"#include "ace/Log_Msg.h"#include "ace/SOCK_Connector.h"#include "ace/INET_Addr.h"#define SIZE_BUF 128#define NO_ITERATIONS 5class Client{public:    Client(char *hostname, int port):remote_addr_(port,hostname)    {      data_buf_="Hello from Client";    }    //Uses a connector component `connector_’ to connect to a    //remote machine and pass the connection into a stream    //component client_stream_    int connect_to_server()    {      // Initiate blocking connection with server.      ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting connect to %s:%d ",            remote_addr_.get_host_name(),remote_addr_.get_port_number()));      if (connector_.connect (client_stream_, remote_addr_) == -1)            ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p ","connection failed"),-1);      else            ACE_DEBUG ((LM_DEBUG,"(%P|%t) connected to %s ",            remote_addr_.get_host_name ()));      return 0;    }    //Uses a stream component to send data to the remote host.    int send_to_server()    {      // Send data to server      for(int i=0;i<NO_ITERATIONS; i++)      {            if (client_stream_.send_n (data_buf_,                ACE_OS::strlen(data_buf_)+1, 0) == -1)            {                ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p ","send_n"),0);                break;            }      }      //Close down the connection      close();      return 0;    }    //Close down the connection properly.    int close()    {      if (client_stream_.close () == -1)            ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p ","close"),-1);      else            return 0;    }private:    ACE_SOCK_Stream client_stream_;    ACE_INET_Addr remote_addr_;    ACE_SOCK_Connector connector_;    char *data_buf_;};int run_main (int argc, ACE_TCHAR *argv[]);intACE_TMAIN (int argc, ACE_TCHAR *argv[]){    return run_main (argc, argv);}int run_main (int argc, ACE_TCHAR *argv[]){    if(argc<3)    {      ACE_DEBUG((LM_DEBUG,"Usage %s <hostname> <port_number> ", argv));      ACE_OS::exit(1);    }    Client client(argv,ACE_OS::atoi(argv));    client.connect_to_server();    client.send_to_server();    return 0;}  
 
页: [1]
查看完整版本: ACE 入门