1 #include "ace/INET_Addr.h"
2 #include "ace/SOCK_Stream.h"
3 #include "ace/SOCK_Connector.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_unistd.h"
7 const char *UPTIME
= "uptime";
8 const char *HUMIDITY
= "humidity";
9 const char *TEMPERATURE
= "temperature";
11 void addCommand (iovec
[], const char *)
14 int ACE_TMAIN (int, ACE_TCHAR
*[])
16 ACE_INET_Addr
srvr (50000, ACE_LOCALHOST
);
17 ACE_SOCK_Connector connector
;
20 ACE_ASSERT (connector
.connect (peer
, srvr
) != -1);
24 // Listing 1 code/ch06
26 send
[0].iov_base
= const_cast<char *> ("up");
28 send
[1].iov_base
= const_cast<char *> ("time");
30 send
[2].iov_base
= const_cast<char *> ("\n");
37 // A more clever approach would use something like this:
38 // Where the addCommand() method allocates and populates
39 // the query array from a set of global commands.
41 // Listing 2 code/ch06
43 addCommand (query
, UPTIME
);
44 addCommand (query
, HUMIDITY
);
45 addCommand (query
, TEMPERATURE
);
46 peer
.sendv (query
, 3);
49 // Listing 3 code/ch06
51 receive
[0].iov_base
= new char [32];
52 receive
[0].iov_len
= 32;
53 receive
[1].iov_base
= new char [64];
54 receive
[1].iov_len
= 64;
56 bc
= peer
.recvv (receive
, 2);
59 // Listing 4 code/ch06
60 for (int i
= 0; i
< 2 && bc
> 0; ++i
)
62 size_t wc
= receive
[i
].iov_len
;
63 if (static_cast<size_t> (bc
) < wc
)
64 wc
= static_cast<size_t> (bc
);
65 ACE_OS::write (ACE_STDOUT
, receive
[i
].iov_base
, wc
);
66 bc
-= receive
[i
].iov_len
;
68 (reinterpret_cast<char *> (receive
[i
].iov_base
));
72 // Listing 5 code/ch06
73 peer
.send_n ("uptime\n", 7);
75 peer
.recvv (&response
);
76 ACE_OS::write (ACE_STDOUT
, response
.iov_base
, response
.iov_len
);
77 delete [] reinterpret_cast<char *> (response
.iov_base
);