生活随笔
收集整理的這篇文章主要介紹了
用ACE的Reactor模式实现网络通讯的例子
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用ACE的Reactor模式實(shí)現(xiàn)網(wǎng)絡(luò)通訊的例子,不羅嗦,直接上代碼。
服務(wù)器代碼:
[cpp] view plain
copy #include?<ace/Reactor.h>??#include?<ace/SOCK_Connector.h>???#include?<ace/SOCK_Acceptor.h>???#include?<ace/Auto_Ptr.h>??????class?ClientService?:?public?ACE_Event_Handler??{??public:??????ACE_SOCK_Stream?&peer?(void)?{?return?this->sock_;?}????????int?open?(void)??????{????????????????????return?this->reactor?()->register_handler(this,?ACE_Event_Handler::READ_MASK);??????}????????virtual?ACE_HANDLE?get_handle?(void)?const?{?return?this->sock_.get_handle?();?}????????virtual?int?handle_input?(ACE_HANDLE?fd?)??????{??????????int?rev?=?peer().recv(buf,sizeof(buf));??????????if(rev<=0)??????????????return?-1;??????????buf[rev]?=?'\0';??????????printf("recv:?%s",buf);??????????strcpy(buf,"hello,Client\n");??????????peer().send(buf,strlen(buf));???????????return?0;??????}??????????virtual?int?handle_close?(ACE_HANDLE,?ACE_Reactor_Mask?mask)??????{??????????if?(mask?==?ACE_Event_Handler::WRITE_MASK)??????????????return?0;??????????mask?=?ACE_Event_Handler::ALL_EVENTS_MASK?|??????????????ACE_Event_Handler::DONT_CALL;??????????this->reactor?()->remove_handler?(this,?mask);??????????this->sock_.close?();??????????delete?this;??????????????return?0;??????}????protected:??????char?buf[100];??????ACE_SOCK_Stream?sock_;??};??????class?ClientAcceptor?:?public?ACE_Event_Handler??{??public:??????virtual?~ClientAcceptor?(){this->handle_close?(ACE_INVALID_HANDLE,?0);}????????int?open?(const?ACE_INET_Addr?&listen_addr)??????{??????????if?(this->acceptor_.open?(listen_addr,?1)?==?-1)??????????{??????????????printf("open?port?fail\n");??????????????return?-1;??????????}????????????????????return?this->reactor?()->register_handler(this,?ACE_Event_Handler::ACCEPT_MASK);??????}????????virtual?ACE_HANDLE?get_handle?(void)?const??????{?return?this->acceptor_.get_handle?();?}????????virtual?int?handle_input?(ACE_HANDLE?fd?)??????{??????????ClientService?*client?=?new?ClientService();??????????auto_ptr<ClientService>?p?(client);????????????if?(this->acceptor_.accept?(client->peer?())?==?-1)??????????{??????????????printf("accept?client?fail\n");??????????????return?-1;??????????}??????????p.release?();??????????client->reactor?(this->reactor?());??????????if?(client->open?()?==?-1)??????????????client->handle_close?(ACE_INVALID_HANDLE,?0);??????????return?0;??????}????????????virtual?int?handle_close?(ACE_HANDLE?handle,??????????ACE_Reactor_Mask?close_mask)??????{??????????if?(this->acceptor_.get_handle?()?!=?ACE_INVALID_HANDLE)??????????{??????????????ACE_Reactor_Mask?m?=?ACE_Event_Handler::ACCEPT_MASK?|??????????????????ACE_Event_Handler::DONT_CALL;??????????????this->reactor?()->remove_handler?(this,?m);??????????????this->acceptor_.close?();??????????}??????????return?0;??????}????protected:??????ACE_SOCK_Acceptor?acceptor_;??};????int?main(int?argc,?char?*argv[])???{??????ACE_INET_Addr?addr(3000,"127.0.0.1");??????ClientAcceptor?server;??????server.reactor(ACE_Reactor::instance());??????server.open(addr);????????while(true)??????{??????????ACE_Reactor::instance()->handle_events();???????}????????return?0;???}???
客戶端代碼:
[cpp] view plain
copy #include?"ace/Reactor.h"??#include?"ace/SOCK_Connector.h"????#include?<string>??#include?<iostream>??using?namespace?std;????class?MyClient:public?ACE_Event_Handler???{??public:??????bool?open()??????{??????????ACE_SOCK_Connector?connector;??????????ACE_INET_Addr?addr(3000,"127.0.0.1");??????????ACE_Time_Value?timeout(5,0);??????????if(connector.connect(peer,addr,&timeout)?!=?0)??????????{??????????????cout<<endl<<"connect?fail.";??????????????return?false;??????????}??????????ACE_Reactor::instance()->register_handler(this,ACE_Event_Handler::READ_MASK);??????????cout<<endl<<"connected.";??????????strcpy(buf,?"hello,Server\n");??????????peer.send(buf,strlen(buf));??????????return?true;??????}????????ACE_HANDLE?get_handle(void)?const??????{??????????return?peer.get_handle();??????}????????int?handle_input?(ACE_HANDLE?fd)??????{??????????int?rev=0;??????????ACE_Time_Value?timeout(5,0);??????????if((rev=peer.recv(buf,sizeof(buf),&timeout))>0)??????????{??????????????buf[rev]='\0';??????????????cout<<endl<<"recv:?"<<buf<<endl;??????????}??????????return?3;??????}????private:??????ACE_SOCK_Stream?peer;??????char?buf[100];??};????int?main(int?argc,?char?*argv[])???{??????MyClient?client;??????client.open();????????while(true)??????{??????????ACE_Reactor::instance()->handle_events();???????}????????return?0;???}??
分別編譯運(yùn)行(先運(yùn)行服務(wù)端,后運(yùn)行客戶端)下面是執(zhí)行結(jié)果。
服務(wù)器:
recv: hello,Server
客戶端:
connected.
recv: hello,Client
?
參考:
http://www.cnblogs.com/TianFang/archive/2006/12/13/591332.html
http://www.cnblogs.com/TianFang/archive/2006/12/18/595938.html
總結(jié)
以上是生活随笔為你收集整理的用ACE的Reactor模式实现网络通讯的例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。