// serverMain.cpp
//配完后测试ACE是否可用
// 如果是WIN32平台上报错
// f:\ace\ace_wrappers\ace\config-win32-common.h(23):
// fatal error C1189: #error : Please define WIN32 in your project settings.
// 可以文件头添加WIN32的预定义解决
#ifndef WIN32
#define WIN32
#endif
// 针对Debug版和Release版进行不同预处理
#ifdef _DEBUG
#pragma comment(lib,"ACEd.lib")
#else
#pragma comment(lib,"ACE.lib")
#endif
#include <ace\Log_Msg.h>
#include <ace\Time_Value.h>
#include <ace\SOCK_Acceptor.h>
#include <ace\SOCK_Stream.h>
#include <ace\OS_NS_stdlib.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\n", "Error in recv"));
else
{
data_buf_[byte_count]=0;
ACE_DEBUG((LM_DEBUG,"Server received %s \n",data_buf_));
}
}
// Close new endpoint
if (new_stream_.close () == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "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\n","Error in get_local_addr"),1);
ACE_DEBUG ((LM_DEBUG,"Starting server at port %d\n",
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\n", "accept"));
continue;
}
else
{
ACE_DEBUG((LM_DEBUG,
"Connection established with remote %s:%d\n",
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 main (int argc, char *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;
}
1. 获取ACE-6.0.0.tar.gz
下载地址:http://download.dre.vanderbilt.edu/
2. 设置环境变量
环境变量”在其中添加一个名为ACE_ROOT的系统环境变量,值为刚才ACE解压的路径“E:\ACE_wrappers”。
在“用户环境变量”中添加变量名:Path,变量值:%ACE_ROOT%\lib,这样能够保证系统找到ace生成的动态链接库。
3. 编译ACE,生成dll,lib
ACE_ROOT\ace目录下新建config.h文件,文件内容:
// config.h
#define ACE_HAS_STANDARD_CPP_LIBRARY 1 // 用于标准C++跨平台
#include "ace/config-win32.h" // 在WIN32环境下使用ACE
#define ACE_USE_WCHAR
#define ACE_HAS_WCHAR // 支持unicode
打开ACE_ROOT\ace目录下项目ace_vc解决方案
vs2010打开ace_vc10.sln
vs2008打开ace_vc9.sln
vs2005打开ace_vc8.sln
解决方案-vc++目录 ,在右侧列表中选择“包含文件”,添加$(ACE_ROOT),
在右侧目录中选择“库文件”,添加$(ACE_ROOT)\lib。