summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_2/ex02.html
blob: 69891f26f1eaa59a025df096043fd38383107564 (plain)
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
<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 2</TITLE>
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE&nbsp;Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">//// &nbsp;Chapter:&nbsp;"IPC&nbsp;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:&nbsp;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>&nbsp;remote_addr_.set_port_number(port);
<BR>&nbsp;data_buf_=new char[SIZE_BUF];
<BR>&nbsp;}

<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>&nbsp; <FONT COLOR="#FF0000">// Initiate blocking connection with server.</FONT>
<BR>&nbsp; ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting connect to %s:%d\n",
<BR>&nbsp;&nbsp;&nbsp; remote_addr_.get_host_name(),remote_addr_.get_port_number()));
<BR>&nbsp; if (connector_.connect (client_stream_, remote_addr_) == -1)
<BR>&nbsp;&nbsp;&nbsp; ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","connection
failed"),-1);
<BR>&nbsp; else
<BR>&nbsp;&nbsp;&nbsp; ACE_DEBUG ((LM_DEBUG,"(%P|%t) connected to %s\n",
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remote_addr_.get_host_name ()));
<BR>&nbsp; return 0;
<BR>&nbsp; }

<P><FONT COLOR="#FF0000">//Uses a stream component to send data to the
remote host.</FONT>
<BR>int send_to_server(){
<BR>&nbsp;<FONT COLOR="#FF0000"> // Send data to server</FONT>
<BR>&nbsp;ACE_OS::sprintf(data_buf_,"Hello from Client");
<BR>&nbsp;for(int i=0;i&lt;NO_ITERATIONS; i++){
<BR>&nbsp; if (client_stream_.send_n
<BR>&nbsp;&nbsp;&nbsp; (data_buf_, ACE_OS::strlen(data_buf_), 0) == -1){
<BR>&nbsp; ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","send_n"),0);
<BR>&nbsp; break;
<BR>&nbsp; }
<BR>&nbsp;}
<BR>&nbsp;<FONT COLOR="#FF0000">//Close down the connection</FONT>
<BR>&nbsp;close();
<BR>}

<P><FONT COLOR="#FF0000">//Close down the connection properly.</FONT>
<BR>int close(){
<BR>&nbsp;if (client_stream_.close () == -1)
<BR>&nbsp; ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","close"),-1);
<BR>&nbsp;else
<BR>&nbsp; return 0;
<BR>&nbsp; }

<P>private:
<BR>&nbsp;ACE_SOCK_Stream client_stream_;
<BR>&nbsp;ACE_INET_Addr remote_addr_;
<BR>&nbsp;ACE_SOCK_Connector connector_;
<BR>&nbsp;char *data_buf_;
<BR>};

<P>int main (int argc, char *argv[]){
<BR>&nbsp;if(argc&lt;3){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,?Usage egX &lt;hostname> &lt;port_number>\n?));
<BR>&nbsp; ACE_OS::exit(1);
<BR>&nbsp; }
<BR>&nbsp;Client client(argv[1],ACE_OS::atoi(argv[2]));
<BR>&nbsp;client.connect_to_server();
<BR>&nbsp;client.send_to_server();
<BR>}

<P>&nbsp;<A HREF="ex03.html">Next Example</A>
</BODY>
</HTML>