您好,欢迎访问三七文档
UmarSyyidusyyid@hns.com@bitsmart.comJamesCEJohnsonjcej@lads.comAaronValdiviaavaldivia@hns.com1DouglasC.Schmidtschmidt@cs.wustl.eduThomasJordanace@programmer.netErikKoerbererik.koerber@siemens.atMartinKrumpoleckrumpo@pobox.skFredKuhnsfredk@tango.cs.wustl.eduSusanLiebeskindshl@cc.gatech.eduAndyBellafaireamba@callisto.eci-esyst.comMarinamarina@cs.wustl.eduJean-PaulGentyjpgenty@sesinsud.com21ACEACE(AdaptiveCommunicationEnvironment)ACEACEzz(IPC)zzzzzzzzzz1.1ACE1-1ACEACEzOSzC++z31-1ACE1.1.1OSOSOSAPIACEACEACEACEOSACEACEOSOSVxWorksChorusLynxOSpSoSUNIXSunOS4.x5.x;SGIIRIX5.x6.x;HP-UX9.x,10.x11.x;DECUNIX3.x4.x;AIX3.x4.x;DG/UX;Linux;SCO;UnixWare;NetBSDFreeBSDWin32MSVC++BorlandC++WinNT3.5.x4.xWin95WinCEMVSOpenEdition1.1.2C++C++C++C++ACE50%C++zACEOSAPIBarrierGuardzIPCACEC++OSIPCACEIPCBSDsocketTLIUNIXFIFOWin32ACEOS4zACEACEzACEtimerwheelVxWorks,Win32/Pentium,AIXSolarisProfileTimerzACESTLMapHash_MapSetListzACEOSzACEAPII/OI/OzACEOSAPI1.1.3ACEACEACEACEzI/OACEReactorzACEConnectorAcceptorACEzACEStreamlayeredhierarchicz5ACEServiceConfiguratorACEACEACEC++ACE62IPCSAPsocketTLISTREAMFIFOIPCACEIPCSAPIPCSAP2.1IPCSAP2-1IPCSAPIPCIPCSAP2-1ACE_IPC_SAPIPCACEIPCSAPIPCACE_SOCKBSDsocketACE_TLITLI2.2socketACE_SOCKACE_SOCKBSDsocketInternetUNIXzDgramStreamDgramUDPStreamTCPzAcceptorConnectorStreamAcceptorConnectorAcceptorBSDaccept()ConnectorBSDconnect()Stream72-1ACE_SOCK_AcceptorBSDaccept()listen()ACE_SOCK_ConnectorBSDconnect()ACE_SOCK_DgramUDPsendto()receivefrom()send()recv()ACE_SOCK_IOsend()recv()write()ACE_SOCK_StreamACE_SOCK_CODgramACE_SOCK_StreamTCPACE_SOCK_IOACE_SOCK_CODgramconnecteddatagramACE_SOCK_IOopen()bind()UDPACE_SOCK_Dgram_Mcast(multicast)ACE_SOCK_Dgram_Bcast(broadcast)2-1ACE_SOCKIPC_SAPACE2.2.1ACEACEACE_SOCK_StreamACE_LSOCK_StreamTCP/IPUNIXsocketTCP/IPACE_SOCK_ConnectorACE_SOCK_AcceptorUNIXsocketACE_LSOCK_ConnectorACE_LSOCK_AcceptorAcceptorBSDaccept()ConnectorBSDconnect()2-1#includeace/SOCK_Acceptor.h#includeace/SOCK_Stream.h#defineSIZE_DATA188#defineSIZE_BUF1024#defineNO_ITERATIONS5classServer{public:Server(intport):server_addr_(port),peer_acceptor_(server_addr_){data_buf_=newchar[SIZE_BUF];}//Handletheconnectiononceithasbeenestablished.Herethe//connectionishandledbyreadingSIZE_DATAamountofdatafromthe//remoteandthenclosingtheconnectionstreamdown.inthandle_connection(){//Readdatafromclientfor(inti=0;iNO_ITERATIONS;i++){intbyte_count=0;if((byte_count=new_stream_.recv_n(data_buf_,SIZE_DATA,0))==-1)ACE_ERROR((LM_ERROR,%p\n,Errorinrecv));else{data_buf_[byte_count]=0;ACE_DEBUG((LM_DEBUG,Serverreceived%s\n,data_buf_));}}//Closenewendpointif(new_stream_.close()==-1)ACE_ERROR((LM_ERROR,%p\n,close));return0;}//Usetheacceptorcomponentpeer_acceptor_toaccepttheconnection//intotheunderlyingstreamnew_stream_.Aftertheconnectionhasbeen//establishedcallthehandle_connection()method.intaccept_connections(){if(peer_acceptor_.get_local_addr(server_addr_)==-1)ACE_ERROR_RETURN((LM_ERROR,%p\n,Erroringet_local_addr),1);ACE_DEBUG((LM_DEBUG,Startingserveratport%d\n,server_addr_.get_port_number()));9//Performstheiterativeserveractivities.while(1){ACE_Time_Valuetimeout(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,Connectionestablishedwithremote%s:%d\n,client_addr_.get_host_name(),client_addr_.get_port_number()));//Handletheconnectionhandle_connection();}}}private:char*data_buf_;ACE_INET_Addrserver_addr_;ACE_INET_Addrclient_addr_;ACE_SOCK_Acceptorpeer_acceptor_;ACE_SOCK_Streamnew_stream_;};intmain(intargc,char*argv[]){if(argc2){ACE_ERROR((LM_ERROR,Usage%sport_num,argv[0]));ACE_OS::exit(1);}Serverserver(ACE_OS::atoi(argv[1]));server.accept_connections();}ServerServeraccept_connections()ACE_SOCK_AcceptorACE_SOCK_Streamnew_stream_accept()10send()recv()ACE_INET_Addraccept()handle_connection()close()socket2-2#includeace/SOCK_Connector.h#includeace/INET_Addr.h#defineSIZE_BUF128#defineNO_ITERATIONS5classClient{public:Client(char*hostname,intport):remote_addr_(port,hostname){data_buf_=HellofromClient;}//Usesaconnectorcomponent`connector_’toconnecttoa//remotemachineandpasstheconnectionintoastream//componentclient_stream_intconnect_to_server(){//Initiateblockingconnectionwithserver.ACE_DEBUG((LM_DEBUG,(%P|%t)Startingconnectto%s:%d\n,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\n,connectionfailed),-1);elseACE_DEBUG((LM_DEBUG,(%P|%t)connectedto%s\n,remote_addr_.get_host_name()));return0;}//Usesastreamcomponenttosenddatatotheremotehost.intsend_to_server(){//Senddatatoserver11for(inti=0;iNO_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\n,send_n),0);break;}}//Closedowntheconnectionclose();}//Closedowntheconnectionproperly.intclose(){if(client_stream_.close()==-1)ACE_ERROR_RETURN((LM_ERROR,(%P|%t)%p\n,close),-1);elsereturn0;}private:
本文标题:ACE程序员教程
链接地址:https://www.777doc.com/doc-5064867 .html