Sophie

Sophie

distrib > Mandriva > 10.0 > i586 > media > contrib > by-pkgid > 21280410b6ea906d791d7a12afae2579 > files > 394

libace5-doc-5.4-2mdk.i586.rpm

<HTML>
<!-- ex04.html,v 1.4 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 4</TITLE>
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; 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">////&nbsp; 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="#CC0000">//Example 4</FONT>
<BR><FONT COLOR="#FF0000">//Client</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
28</FONT>
<BR>class Client{
<BR>public:
<BR>Client(char * remote_addr,int port)
<BR>&nbsp;:remote_addr_(remote_addr),
<BR>&nbsp; local_addr_((u_short)0),local_(local_addr_){
<BR>&nbsp;&nbsp;&nbsp; data_buf = new char[DATA_BUFFER_SIZE];
<BR>&nbsp; remote_addr_.set_port_number(port);
<BR>&nbsp; }
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">//Accept data from the remote host using the
datgram component local_</FONT>
<BR>int accept_data(){
<BR>&nbsp;if(local_.recv(data_buf,DATA_BUFFER_SIZE,remote_addr_)!=-1){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG, "Data received from remote server %s
<BR>&nbsp;&nbsp;&nbsp;&nbsp; was: %s \n" ,remote_addr_.get_host_name(),
data_buf));
<BR>&nbsp; return 0;
<BR>&nbsp; }
<BR>&nbsp;else
<BR>&nbsp; return -1;
<BR>&nbsp;}

<P><FONT COLOR="#FF0000">//Send data to the remote. Once data has been
sent wait for a reply from</FONT>
<BR><FONT COLOR="#FF0000">//the server.</FONT>
<BR>int send_data(){
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Preparing to send data to server %s:%d\n",
<BR>&nbsp;&nbsp; remote_addr_.get_host_name(),remote_addr_.get_port_number()));
<BR>&nbsp;ACE_OS::strcpy(data_buf,"Client says hello");
<BR>&nbsp;
<BR>&nbsp;while(local_.send
<BR>&nbsp;&nbsp;&nbsp; (data_buf,ACE_OS::strlen(data_buf)+1,remote_addr_)!=-1){
<BR>&nbsp; ACE_OS::sleep(1);
<BR>&nbsp; if(accept_data()==-1)
<BR>&nbsp;&nbsp; break;
<BR>&nbsp; }
<BR>&nbsp; return -1;
<BR>&nbsp; }

<P>private:
<BR>&nbsp;char *data_buf;
<BR>&nbsp;ACE_INET_Addr remote_addr_;
<BR>&nbsp;ACE_INET_Addr local_addr_;
<BR>&nbsp;ACE_SOCK_Dgram local_;
<BR>};

<P>int main(int argc, char *argv[]){
<BR>if(argc&lt;3){
<BR>&nbsp;ACE_OS::printf("Usage: Client hostname port_number \n");
<BR>&nbsp;ACE_OS::exit(1);
<BR>&nbsp;}
<BR>Client client(argv[1],ACE_OS::atoi(argv[2]));
<BR>client.send_data();
<BR>return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;<A HREF="ex05.html">Next Example</A>
</BODY>
</HTML>