@H_403_4@两种实现方法
1. @H_403_4@Perclient Per Thread @H_403_4@(每个连接每个线程来处理)
这种方式比较简单,易于实现。缺点是如果有大量的连接,必然占用过量的系统资源。
代码:
#ifdef_DEBUG
#pragmacomment(lib,"ACED.lib")
#else
#pragmacomment(lib,"ACE.lib")
#endif
#include<ace/OS_main.h>
#include<ace/ACE.h>
#include<ace/Log_Msg.h>
#include<ace/SOCK_Acceptor.h>
#include<ace/Thread.h>
DWORDworker( void*arg)
{
ACE_SOCK_Stream*pStream=(ACE_SOCK_Stream*)arg;
charbuffer[32]={0};
intlen;
while((len=pStream->recv(buffer,sizeof(buffer)-1))>0)
{
if(pStream->send_n(buffer,len)<=0)
{
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("(%P|%t)%p\n"),
ACE_TEXT("sendFailed")));
break;
}
}
ACE_INET_Addrraddr;
pStream->get_remote_addr(raddr);
ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%P|%t)close:%s%d\n"),raddr.get_host_addr(),raddr.get_port_number()));
pStream->close();
deletepStream;
return0;
}
intmain( intargc,255)">char*argv[])
{
ACE_INET_Addraddr(1500);
ACE_SOCK_Acceptorserver;
if(server.open(addr)==-1)
{
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("bindFailed")));
return1;
}
ACE_SOCK_Stream*pStream=0;
while((pStream= newACE_SOCK_Stream())&&server.accept(*pStream)!=-1)
{
ACE_INET_Addrraddr;
pStream->get_remote_addr(raddr);
ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%P|%t)connect:%s%d\n"),raddr.get_port_number()));
ACE_Thread::spawn(worker,pStream);
}
server.close();
return0;
}
#pragmacomment(lib,"ACED.lib")
#else
#pragmacomment(lib,"ACE.lib")
#endif
#include<ace/OS_main.h>
#include<ace/ACE.h>
#include<ace/Log_Msg.h>
#include<ace/SOCK_Acceptor.h>
#include<ace/Thread.h>
DWORDworker( void*arg)
{
ACE_SOCK_Stream*pStream=(ACE_SOCK_Stream*)arg;
charbuffer[32]={0};
intlen;
while((len=pStream->recv(buffer,sizeof(buffer)-1))>0)
{
if(pStream->send_n(buffer,len)<=0)
{
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("(%P|%t)%p\n"),
ACE_TEXT("sendFailed")));
break;
}
}
ACE_INET_Addrraddr;
pStream->get_remote_addr(raddr);
ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%P|%t)close:%s%d\n"),raddr.get_host_addr(),raddr.get_port_number()));
pStream->close();
deletepStream;
return0;
}
intmain( intargc,255)">char*argv[])
{
ACE_INET_Addraddr(1500);
ACE_SOCK_Acceptorserver;
if(server.open(addr)==-1)
{
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("bindFailed")));
return1;
}
ACE_SOCK_Stream*pStream=0;
while((pStream= newACE_SOCK_Stream())&&server.accept(*pStream)!=-1)
{
ACE_INET_Addrraddr;
pStream->get_remote_addr(raddr);
ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%P|%t)connect:%s%d\n"),raddr.get_port_number()));
ACE_Thread::spawn(worker,pStream);
}
server.close();
return0;
}
说明:
ACE_Thread用来启动thread,当有client连上来的时候,启动worker thread来处理。
2. @H_403_4@事件模型
socket里面是有select机制,ACE也提供了这种方式。
概念:
Event_Handler:有事件来临的时候的处理器
Reactor:事件管理,负责管理Event_Hanlde和分发事件
Acceptor负责连接Event_Handle,Reactor和Sock_Acceptor
#endif
#include<ace/OS_main.h>
#include<ace/ACE.h>
#include<ace/Svc_Handler.h>
#include<ace/Acceptor.h>
#include<ace/Log_Msg.h>
#include<ace/SOCK_Acceptor.h>
#include<ace/Thread.h>
classMy_Svc_Handler:
publicACE_Svc_Handler<ACE_SOCK_STREAM,ACE_NULL_SYNCH>
{
public:
intopen( void*)
{
ACE_INET_Addrraddr;
peer().get_remote_addr(raddr);
ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%P|%t)connect:%s%d%d\n"),raddr.get_port_number(),255)">this));
reactor()->register_handler( this,ACE_Event_Handler::READ_MASK);
return0;
}
inthandle_input(ACE_HANDLE)
{
charbuffer[32]={0};
intlen=peer().recv(buffer,255)">sizeof(buffer)-1);
if(len>0)
{
if(peer().send_n(buffer,255)">return-1; // mustreturn-1tocallhandle_close
}
return0;
}
else
{
}
}
inthandle_close(ACE_HANDLEhandle,ACE_Reactor_Maskclose_mask)
{
ACE_INET_Addrraddr;
peer().get_remote_addr(raddr);
ACE_DEBUG((LM_DEBUG,raddr.get_port_number()));
peer().close();
return0;
}
};
typedefACE_Acceptor<My_Svc_Handler,ACE_SOCK_ACCEPTOR>MyAcceptor;
char*argv[])
{
ACE_INET_Addraddr(1500);
MyAcceptorserver;
return1;
}
while(1){
ACE_Reactor::instance()->handle_events();
}
server.close();
说明:
1.使用ACE_Acceptor模板类来定义自己的Acceptor
2.ACE_Reactor::instance()->handle_events()来实现事件分发
3.在Event_Handle里面使用reactor()->register_handler(this,ACE_Event_Handler::READ_MASK);来注册要关心的事件
4.在Event_Handle中使用peer()来获得Sock_Stream来发送接受数据。 原文链接:https://www.f2er.com/react/308254.html