Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Sockets / Iovec.cpp
blob3130be3f5d12839c2de9f0cf0ed7c75fa2733b92
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;
18 ACE_SOCK_Stream peer;
20 ACE_ASSERT (connector.connect (peer, srvr) != -1);
22 ssize_t bc;
24 // Listing 1 code/ch06
25 iovec send[4];
26 send[0].iov_base = const_cast<char *> ("up");
27 send[0].iov_len = 2;
28 send[1].iov_base = const_cast<char *> ("time");
29 send[1].iov_len = 4;
30 send[2].iov_base = const_cast<char *> ("\n");
31 send[2].iov_len = 1;
33 peer.sendv (send, 3);
34 // Listing 1
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
42 iovec query[3];
43 addCommand (query, UPTIME);
44 addCommand (query, HUMIDITY);
45 addCommand (query, TEMPERATURE);
46 peer.sendv (query, 3);
47 // Listing 2
49 // Listing 3 code/ch06
50 iovec receive[2];
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);
57 // Listing 3
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;
67 delete []
68 (reinterpret_cast<char *> (receive[i].iov_base));
70 // Listing 4
72 // Listing 5 code/ch06
73 peer.send_n ("uptime\n", 7);
74 iovec response;
75 peer.recvv (&response);
76 ACE_OS::write (ACE_STDOUT, response.iov_base, response.iov_len);
77 delete [] reinterpret_cast<char *> (response.iov_base);
78 // Listing 5
80 peer.close ();
82 return (0);