ACE配置及問題總結
ACEACE自適配通信環境 (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)。
下載地址:
注意我這里下載的是.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
這時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_)+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[]);

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;
}