<HTML> <!-- ex03.html,v 1.3 2001/01/31 17:22:27 shuston 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]"> <TITLE>Example 3</TITLE> </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><FONT COLOR="#FF0000"></FONT> <P><FONT COLOR="#CC0000">// Example 3</FONT><FONT COLOR="#FF0000"></FONT> <P><FONT COLOR="#FF0000">//Server</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/OS.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Dgram.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/INET_Addr.h"</FONT> <P><FONT COLOR="#000099">#define </FONT><FONT COLOR="#663366">DATA_BUFFER_SIZE 1024</FONT> <BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">SIZE_DATA 18</FONT> <P>class Server{ <BR>public: <BR>Server(int local_port) <BR> :local_addr_(local_port),local_(local_addr_){ <BR> data_buf = new char[DATA_BUFFER_SIZE]; <BR> } <BR><FONT COLOR="#FF0000">//Expect data to arrive from the remote machine. Accept it and display it.</FONT> <BR><FONT COLOR="#FF0000">// After recieveing data immediately send some data back to the remote.</FONT> <BR>int accept_data(){ <BR> while(local_.recv(data_buf,DATA_BUFFER_SIZE,remote_addr_)!=-1){ <BR> ACE_DEBUG((LM_DEBUG, "Data received from remote %s was %s \n" <BR> ,remote_addr_.get_host_name(), data_buf)); <BR> ACE_OS::sleep(1); <BR> if(send_data()==-1) break; <BR> } <BR> return -1; <BR> } <BR> <BR> <P><FONT COLOR="#FF0000">//Method used to send data to the remote using the datagram component local_</FONT> <BR>int send_data(){ <BR> ACE_DEBUG((LM_DEBUG,"Preparing to send reply to client %s:%d\n", <BR> remote_addr_.get_host_name(),remote_addr_.get_port_number())); <BR> ACE_OS::strcpy(data_buf,"Server says hello to you too"); <BR> if( <BR> local_.send(data_buf, ACE_OS::strlen(data_buf)+1,remote_addr_)==-1) <BR> return -1; <BR> else <BR> return 0; <BR> } <P>private: <BR> char *data_buf; <BR> ACE_INET_Addr remote_addr_; <BR> ACE_INET_Addr local_addr_; <BR> ACE_SOCK_Dgram local_; <BR>}; <P>int main(int argc, char *argv[]){ <BR> Server server(ACE_OS::atoi(argv[1])); <BR> server.accept_data(); <BR> return 0; <BR>} <P> <A HREF="ex04.html">Next Example</A> </BODY> </HTML>