ACE配置

ACE配置及问题总结
ACE ACE 自适配通信环境 (Adaptive Communication Environment) 是面向对象的框架和工具包,它为通信软件实现了核心的并发和分布式模式。 ACE 包含的多种组件可以帮助通信软件的开发获得更好的灵活性、效率、可靠性和可移植性。
       早就在网上看到很多有关ACE有关的介绍,所以准备在接下来的时间里学习一下,主要想在c++通信软件设计方面有所了解。
       下面是我在编译ACE、编写第一个ACE程序过程中碰到的问题,以及如何解决的,希望能对一些初学者能有所帮助。
       首项声明下我的测试环境:
       Windows XP sp2 Professional + VS .NET 2005 Professional +ACE Latest Beta Kit(最新的Beta包)
一、ACE编译
a)         下载ACE源文件,你可以从官方网站下载,很快的,你可以下载最新的发布版本(The latest release kit), 我这里下载的是最新的Beta版本(Latest Beta Kit)。
下载地址:
http://download.dre.vanderbilt.edu/
注意我这里下载的是.zip作为扩展名的版本,这是适用于Windows 2000, XP (MSVC++ 7.1, and 8)。
b)        将你下载的ACE.zip解压缩,假设目录为:
D:/Develop/ACE/ACE_wrappers
在该目录下你可以发现ACE-INSTALL.html文件,这个文件介绍了如何编译ACE,如果你英文不错的话,可以参考该文档。
c)        设置环境变量:
ACE_ROOT:ACE_wrappers所在的目录,这里是D:/Develop/ACE/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
某任情况下你将得到动态和静态两个连接库
                     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内容
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
#pragma  comment (lib,"aced.lib")
#else
#pragma  comment (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 5
class Server
{
public:
    Server (
int port): server_addr_(port),peer_acceptor_(server_addr_)
    
{
        data_buf_
= new char[SIZE_BUF];
    }

    
//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_[byte_count]
=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[]);

int
ACE_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[0]));
        ACE_OS::exit(
1);
    }

    Server server(ACE_OS::atoi(argv[
1]));
    server.accept_connections();
    
return 0;
}
//ClientMain.cpp
#ifdef _DEBUG
#pragma  comment (lib,"aced.lib")
#else
#pragma  comment (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 5
class 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_)
+10== -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[]);

int
ACE_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[0]));
        ACE_OS::exit(
1);
    }

    Client client(argv[
1],ACE_OS::atoi(argv[2]));
    client.connect_to_server();
    client.send_to_server();
    
return 0;
}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值