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
88
89
90
91
92
93
94
95
96
|
<HTML>
<!-- $Id$ -->
<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 2</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="#FF0000">//</FONT><FONT COLOR="#CC0000">Example 2</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Connector.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/INET_Addr.h"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">SIZE_BUF
128</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">NO_ITERATIONS
5</FONT>
<P>class Client{
<BR>public:
<BR>Client(char *hostname, int port):remote_addr_(hostname){
<BR> remote_addr_.set_port_number(port);
<BR> data_buf_=new char[SIZE_BUF];
<BR> }
<P><FONT COLOR="#FF0000">//Uses a connector component connector_ to connect
to a remote machine</FONT>
<BR><FONT COLOR="#FF0000">//and pass the connection into a stream component
client_stream_</FONT>
<BR>int connect_to_server(){
<BR> <FONT COLOR="#FF0000">// Initiate blocking connection with server.</FONT>
<BR> ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting connect to %s:%d\n",
<BR> remote_addr_.get_host_name(),remote_addr_.get_port_number()));
<BR> if (connector_.connect (client_stream_, remote_addr_) == -1)
<BR> ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","connection
failed"),-1);
<BR> else
<BR> ACE_DEBUG ((LM_DEBUG,"(%P|%t) connected to %s\n",
<BR> remote_addr_.get_host_name ()));
<BR> return 0;
<BR> }
<P><FONT COLOR="#FF0000">//Uses a stream component to send data to the
remote host.</FONT>
<BR>int send_to_server(){
<BR> <FONT COLOR="#FF0000"> // Send data to server</FONT>
<BR> ACE_OS::sprintf(data_buf_,"Hello from Client");
<BR> for(int i=0;i<NO_ITERATIONS; i++){
<BR> if (client_stream_.send_n
<BR> (data_buf_, ACE_OS::strlen(data_buf_), 0) == -1){
<BR> ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","send_n"),0);
<BR> break;
<BR> }
<BR> }
<BR> <FONT COLOR="#FF0000">//Close down the connection</FONT>
<BR> close();
<BR>}
<P><FONT COLOR="#FF0000">//Close down the connection properly.</FONT>
<BR>int close(){
<BR> if (client_stream_.close () == -1)
<BR> ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","close"),-1);
<BR> else
<BR> return 0;
<BR> }
<P>private:
<BR> ACE_SOCK_Stream client_stream_;
<BR> ACE_INET_Addr remote_addr_;
<BR> ACE_SOCK_Connector connector_;
<BR> char *data_buf_;
<BR>};
<P>int main (int argc, char *argv[]){
<BR> if(argc<3){
<BR> ACE_DEBUG((LM_DEBUG,?Usage egX <hostname> <port_number>\n?));
<BR> ACE_OS::exit(1);
<BR> }
<BR> Client client(argv[1],ACE_OS::atoi(argv[2]));
<BR> client.connect_to_server();
<BR> client.send_to_server();
<BR>}
<P> <A HREF="ex03.html">Next Example</A>
</BODY>
</HTML>
|