ACE入门

一、ACE编译

a) 下载ACE源文件,你可以从官方网站下载,我的是ACE+TAO+CIAO-6.2.0。

下载地址:

http://download.dre.vanderbilt.edu/

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_vc10.sln,即ACE对应的VC10 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的服务器/客户端通信的代码,略加改动。 

Cpp代码   收藏代码
  1.  //ServerMain.cpp  
  2. #ifdef _DEBUG  
  3. #pragma  comment (lib,"aced.lib")  
  4. #else  
  5. #pragma  comment (lib,"ace.lib")  
  6. #endif  
  7.   
  8. #include "ace/OS.h"  
  9. #include "ace/Log_Msg.h"  
  10. #include "ace/SOCK_Acceptor.h"  
  11. #include "ace/SOCK_Stream.h"  
  12. #define SIZE_DATA 18  
  13. #define SIZE_BUF 1024  
  14. #define NO_ITERATIONS 5  
  15. class Server  
  16. {  
  17. public:  
  18.     Server (int port): server_addr_(port),peer_acceptor_(server_addr_)  
  19.     {  
  20.         data_buf_= new char[SIZE_BUF];  
  21.     }  
  22.     //Handle the connection once it has been established. Here the  
  23.     //connection is handled by reading SIZE_DATA amount of data from the  
  24.     //remote and then closing the connection stream down.  
  25.     int handle_connection()  
  26.     {  
  27.         // Read data from client  
  28.         for(int i=0;i<NO_ITERATIONS;i++)  
  29.         {  
  30.             int byte_count=0;  
  31.             if( (byte_count=new_stream_.recv_n (data_buf_, SIZE_DATA, 0))==-1)  
  32.                 ACE_ERROR ((LM_ERROR, "%p ""Error in recv"));  
  33.             else  
  34.             {  
  35.                 data_buf_[byte_count]=0;  
  36.                 ACE_DEBUG((LM_DEBUG,"Server received %s  ",data_buf_));  
  37.             }  
  38.         }  
  39.         // Close new endpoint  
  40.         if (new_stream_.close () == -1)  
  41.             ACE_ERROR ((LM_ERROR, "%p ""close"));  
  42.         return 0;  
  43.     }  
  44.     //Use the acceptor component peer_acceptor_ to accept the connection  
  45.     //into the underlying stream new_stream_. After the connection has been  
  46.     //established call the handle_connection() method.  
  47.     int accept_connections ()  
  48.     {  
  49.         if (peer_acceptor_.get_local_addr (server_addr_) == -1)  
  50.             ACE_ERROR_RETURN ((LM_ERROR,"%p ","Error in get_local_addr"),1);  
  51.         ACE_DEBUG ((LM_DEBUG,"Starting server at port %d ",  
  52.             server_addr_.get_port_number ()));  
  53.         // Performs the iterative server activities.  
  54.         while(1)  
  55.         {  
  56.             ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);  
  57.             if (peer_acceptor_.accept (new_stream_, &client_addr_, &timeout)== -1)  
  58.             {  
  59.                 ACE_ERROR ((LM_ERROR, "%p ""accept"));  
  60.                 continue;  
  61.             }  
  62.             else  
  63.             {  
  64.                 ACE_DEBUG((LM_DEBUG,  
  65.                     "Connection established with remote %s:%d ",  
  66.                     client_addr_.get_host_name(),client_addr_.get_port_number()));  
  67.                 //Handle the connection  
  68.                 handle_connection();  
  69.             }  
  70.         }  
  71.     }  
  72. private:  
  73.     char *data_buf_;  
  74.     ACE_INET_Addr server_addr_;  
  75.     ACE_INET_Addr client_addr_;  
  76.     ACE_SOCK_Acceptor peer_acceptor_;  
  77.     ACE_SOCK_Stream new_stream_;  
  78. };  
  79. int run_main (int argc, ACE_TCHAR *argv[]);  
  80.   
  81. int  
  82. ACE_TMAIN (int argc, ACE_TCHAR *argv[])  
  83. {  
  84.     return run_main (argc, argv);  
  85. }  
  86. int run_main (int argc, ACE_TCHAR *argv[])  
  87. {  
  88.     if(argc<2)  
  89.     {  
  90.         ACE_ERROR((LM_ERROR,"Usage %s <port_num>", argv[0]));  
  91.         ACE_OS::exit(1);  
  92.     }  
  93.     Server server(ACE_OS::atoi(argv[1]));  
  94.     server.accept_connections();  
  95.     return 0;  
  96. }  
  97.  //ClientMain.cpp  
  98. #ifdef _DEBUG  
  99. #pragma  comment (lib,"aced.lib")  
  100. #else  
  101. #pragma  comment (lib,"ace.lib")  
  102. #endif  
  103.   
  104. #include "ace/OS.h"  
  105. #include "ace/Log_Msg.h"  
  106. #include "ace/SOCK_Connector.h"  
  107. #include "ace/INET_Addr.h"  
  108. #define SIZE_BUF 128  
  109. #define NO_ITERATIONS 5  
  110. class Client  
  111. {  
  112. public:  
  113.     Client(char *hostname, int port):remote_addr_(port,hostname)  
  114.     {  
  115.         data_buf_="Hello from Client";  
  116.     }  
  117.     //Uses a connector component `connector_’ to connect to a  
  118.     //remote machine and pass the connection into a stream  
  119.     //component client_stream_  
  120.     int connect_to_server()  
  121.     {  
  122.         // Initiate blocking connection with server.  
  123.         ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting connect to %s:%d ",  
  124.             remote_addr_.get_host_name(),remote_addr_.get_port_number()));  
  125.         if (connector_.connect (client_stream_, remote_addr_) == -1)  
  126.             ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p ","connection failed"),-1);  
  127.         else  
  128.             ACE_DEBUG ((LM_DEBUG,"(%P|%t) connected to %s ",  
  129.             remote_addr_.get_host_name ()));  
  130.         return 0;  
  131.     }  
  132.     //Uses a stream component to send data to the remote host.  
  133.     int send_to_server()  
  134.     {  
  135.         // Send data to server  
  136.         for(int i=0;i<NO_ITERATIONS; i++)  
  137.         {  
  138.             if (client_stream_.send_n (data_buf_,  
  139.                 ACE_OS::strlen(data_buf_)+1, 0) == -1)  
  140.             {  
  141.                 ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p ","send_n"),0);  
  142.                 break;  
  143.             }  
  144.         }  
  145.         //Close down the connection  
  146.         close();  
  147.         return 0;  
  148.     }  
  149.     //Close down the connection properly.  
  150.     int close()  
  151.     {  
  152.         if (client_stream_.close () == -1)  
  153.             ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p ","close"),-1);  
  154.         else  
  155.             return 0;  
  156.     }  
  157. private:  
  158.     ACE_SOCK_Stream client_stream_;  
  159.     ACE_INET_Addr remote_addr_;  
  160.     ACE_SOCK_Connector connector_;  
  161.     char *data_buf_;  
  162. };  
  163. int run_main (int argc, ACE_TCHAR *argv[]);  
  164.   
  165. int  
  166. ACE_TMAIN (int argc, ACE_TCHAR *argv[])  
  167. {  
  168.     return run_main (argc, argv);  
  169. }  
  170. int run_main (int argc, ACE_TCHAR *argv[])  
  171. {  
  172.     if(argc<3)  
  173.     {  
  174.         ACE_DEBUG((LM_DEBUG,"Usage %s <hostname> <port_number> ", argv[0]));  
  175.         ACE_OS::exit(1);  
  176.     }  
  177.     Client client(argv[1],ACE_OS::atoi(argv[2]));  
  178.     client.connect_to_server();  
  179.     client.send_to_server();  
  180.     return 0;  
  181. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值