<HTML> <!-- ex01.html,v 1.2 2000/06/04 22:02:08 brunsch Exp --> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="Ambreen Ilyas"> <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]"> </HEAD> <BODY> <FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT> <BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers Guide.</FONT> <BR><FONT COLOR="#CC0000">//// Chapter: "IPC SAP" (Interprocess Communication Mechanisms in ACE).</FONT> <BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT> <BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT> <BR><FONT COLOR="#CC0000">//// AUTHOR: Umar Syyid (usyyid@hns.com)</FONT> <BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT> <BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT> <P><FONT COLOR="#FF0000">//Example 1</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Acceptor.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Stream.h"</FONT> <BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">SIZE_DATA 18</FONT> <BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">SIZE_BUF 1024</FONT> <P>class Server{ <P>public: <BR>Server (int port): <BR> server_addr_(port),peer_acceptor_(server_addr_){ <BR> data_buf_= new char[SIZE_BUF]; <BR> } <P><FONT COLOR="#FF0000">//Handle the connection once it has been established.</FONT> <BR><FONT COLOR="#FF0000">//Here the connection is handled by reading SIZE_DATA amount of data</FONT> <BR><FONT COLOR="#FF0000">//from the remote and then closing the connection</FONT> <BR><FONT COLOR="#FF0000">//stream down.</FONT> <BR>int handle_connection(){ <BR> <FONT COLOR="#FF0000"> // Read data from client</FONT> <BR> if(new_stream_.recv_n (data_buf_, SIZE_DATA, 0)==-1) <BR> ACE_ERROR ((LM_ERROR, "%p\n", "Error in recv")); <BR> <P> ACE_DEBUG((LM_DEBUG,"Server recieved %s \n",data_buf_)); <BR> <BR> <FONT COLOR="#FF0000">// Close new endpoint</FONT> <BR> if (new_stream_.close () == -1) <BR> ACE_ERROR ((LM_ERROR, "%p\n", "close")); <BR> return 0; <BR>} <BR><FONT COLOR="#FF0000">//Use the acceptor component peer_acceptor_ to accept the connection</FONT> <BR><FONT COLOR="#FF0000">//into the underlying stream new_stream_. After the connection has been</FONT> <BR><FONT COLOR="#FF0000">//established call the handle_connenction() method.</FONT> <BR>int accept_connections (){ <BR> if (peer_acceptor_.get_local_addr (server_addr_) == -1) <BR> ACE_ERROR_RETURN ((LM_ERROR,"%p\n","Error in get_local_addr"),1); <P> ACE_DEBUG ((LM_DEBUG,"Starting server at port %d\n", <BR> server_addr_.get_port_number ())); <BR> <P> <FONT COLOR="#FF0000">// Performs the iterative server activities.</FONT> <BR> while(1){ <BR> ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT); <BR> if (peer_acceptor_.accept <BR> (new_stream_, &client_addr_, &timeout)== -1){ <BR> ACE_ERROR ((LM_ERROR, "%p\n", "accept")); <BR> continue; <BR> } <BR> else <BR> ACE_DEBUG((LM_DEBUG, <BR> "Connection established with remote %s:%d\n", <BR> client_addr_.get_host_name(),client_addr_.get_port_number())); <BR> <FONT COLOR="#FF0000">//Handle the connection</FONT> <BR> handle_connection(); <BR> } <BR> } <BR> <BR> <P>private: <BR> char *data_buf_; <BR> ACE_INET_Addr server_addr_; <BR> ACE_INET_Addr client_addr_; <BR> ACE_SOCK_Acceptor peer_acceptor_; <BR> ACE_SOCK_Stream new_stream_; <BR> ACE_HANDLE newhandle; <BR>}; <P>int main (int argc, char *argv[]){ <BR> if(argc<2){ <BR> ACE_ERROR((LM_ERROR,"Usage egX <port_num>")); <BR> ACE_OS::exit(1); <BR> } <BR> Server server(ACE_OS::atoi(argv[1])); <BR> server.accept_connections(); <BR>} <P> <A HREF="ex02.html">Next Example</A> </BODY> </HTML>