|
#include "ace/OS.h"
#include "ace/INET_Addr.h"
#include "ace/Reactor.h"
#include "ace/Select_Reactor.h"
#include "ace/Synch.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/Event_Handler.h"
#include "ace/Svc_Handler.h"
#include "ace/Acceptor.h"
class ServerSvcHandler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_MT_SYNCH>
{
public:
ServerSvcHandler()
{
}
~ServerSvcHandler()
{
}
int open(void *);
int handle_input(ACE_HANDLE);
int handle_close(ACE_HANDLE, ACE_Reactor_Mask);
};
int ServerSvcHandler::open(void *)
{
int ret = ACE_Reactor::instance()->register_handler(this, ACE_Event_Handler::READ_MASK);
char szPeer[64];
memset(szPeer, 0, sizeof(szPeer));
ACE_INET_Addr peerAddr;
peer().get_remote_addr(peerAddr);
peerAddr.addr_to_string(szPeer, sizeof(szPeer));
printf("%s enter open\n", szPeer);
}
int ServerSvcHandler::handle_input(ACE_HANDLE h)
{
return 1;
}
int ServerSvcHandler::handle_close(ACE_HANDLE, ACE_Reactor_Mask)
{
return 1;
}
typedef ACE_Acceptor<ServerSvcHandler, ACE_SOCK_ACCEPTOR> Acceptor;
static ACE_THR_FUNC_RETURN event_loop(void* arg)
{
ACE_Reactor *reactor = (ACE_Reactor*)arg;
ACE_Reactor::instance()->owner(ACE_OS::thr_self());
printf("enter event_loop\n");
while (true)
{
ACE_Time_Value tv(2, 0);
reactor->handle_events(&tv);
// printf("in event_loop\n");
}
return 0;
}
Acceptor acceptor;
int main(int argc, char* argv[])
{
ACE::init();
ACE_Select_Reactor select_reactor;
ACE_Reactor default_reactor(&select_reactor);
ACE_Reactor::instance(&default_reactor);
ACE_Thread_Manager::instance()->spawn_n(1, event_loop, ACE_Reactor::instance());
ACE_INET_Addr svrAddr(1234);
int ret = acceptor.open(svrAddr, ACE_Reactor::instance());
printf("acceptor.open ret %d\n", ret);
ACE_Thread_Manager::instance()->wait();
printf("exit main");
return 1;
}
Windows下的客户端连接失败,Linux下用netstat -l 也看不到1234端口在listen,第一次在Linux下用ACE,Linux不太熟,麻烦大牛给看看,多谢多谢 |
|