1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
<HTML>
<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">//// 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="#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> :remote_addr_(remote_addr),
<BR> local_addr_((u_short)0),local_(local_addr_){
<BR> data_buf = new char[DATA_BUFFER_SIZE];
<BR> remote_addr_.set_port_number(port);
<BR> }
<BR>
<BR><FONT COLOR="#FF0000">//Accept data from the remote host using the
datgram component local_</FONT>
<BR>int accept_data(){
<BR> if(local_.recv(data_buf,SIZE_DATA,remote_addr_)!=-1){
<BR> ACE_DEBUG((LM_DEBUG, "Data received from remote server %s
<BR> was: %s \n" ,remote_addr_.get_host_name(),
data_buf));
<BR> return 0;
<BR> }
<BR> else
<BR> return -1;
<BR> }
<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> ACE_DEBUG((LM_DEBUG,"Preparing to send data to server %s:%d\n",
<BR> remote_addr_.get_host_name(),remote_addr_.get_port_number()));
<BR> ACE_OS::sprintf(data_buf,"Client says hello");
<BR>
<BR> while(local_.send
<BR> (data_buf,ACE_OS::strlen(data_buf),remote_addr_)!=-1){
<BR> ACE_OS::sleep(1);
<BR> if(accept_data()==-1)
<BR> break;
<BR> }
<BR> return -1;
<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>if(argc<3){
<BR> ACE_OS::printf("Usage: Client hostname port_number \n");
<BR> ACE_OS::exit(1);
<BR> }
<BR>Client client(argv[1],ACE_OS::atoi(argv[2]));
<BR>client.send_data();
<BR>}
<BR>
<BR> <A HREF="ex05.htm">Next Example</A>
</BODY>
</HTML>
|