1 #include "ace/OS_NS_errno.h"
2 #include "ace/INET_Addr.h"
3 #include "ace/SOCK_Stream.h"
4 #include "ace/SOCK_Connector.h"
5 #include "ace/Log_Msg.h"
6 #include "ace/Time_Value.h"
8 int ACE_TMAIN (int, ACE_TCHAR
*[])
11 * Here we will use the default ctor and the set()
12 * method to configure it. After each set() we will
13 * display the address as a string and then connect
14 * to each respective server. We can reuse the addr
15 * instance once connection has been established.
17 // Listing 1 code/ch06
20 addr.set ("HAStatus", ACE_LOCALHOST);
22 addr.set ("HALog", ACE_LOCALHOST);
28 ACE_TCHAR peerAddress
[64];
30 // Listing 2 code/ch06
31 addr
.set (ACE_TEXT("HAStatus"), ACE_LOCALHOST
);
32 if (addr
.addr_to_string (peerAddress
,
33 sizeof(peerAddress
), 0) == 0)
36 ACE_TEXT ("(%P|%t) Connecting to %s\n"),
41 // Listing 3 code/ch06
42 ACE_SOCK_Stream status
;
43 ACE_OS::last_error(0);
44 ACE_SOCK_Connector
statusConnector (status
, addr
);
45 if (ACE_OS::last_error())
46 ACE_ERROR_RETURN ((LM_ERROR
,
48 ACE_TEXT ("status")), 100);
51 addr
.set (ACE_TEXT("HALog"), ACE_LOCALHOST
);
52 if (addr
.addr_to_string (peerAddress
,
53 sizeof(peerAddress
), 0) == 0)
56 ACE_TEXT ("(%P|%t) Connecting to %s\n"),
60 // Listing 4 code/ch06
61 ACE_SOCK_Connector logConnector
;
62 ACE_Time_Value
timeout (10);
64 if (logConnector
.connect (log
, addr
, &timeout
) == -1)
66 if (ACE_OS::last_error() == ETIME
)
69 ACE_TEXT ("(%P|%t) Timeout while ")
70 ACE_TEXT ("connecting to log server\n")));
83 * We generally let the OS pick our local port number but
84 * if you want, you can choose that also:
85 // Listing 5 code/ch06
86 ACE_SOCK_Connector logConnector;
87 ACE_INET_Addr local (4200, ACE_LOCALHOST);
88 if (logConnector.connect (log, addr, 0, local) == -1)
97 // Listing 6 code/ch06
98 ACE_Time_Value
sendTimeout (0, 5);
99 if (status
.send_n ("uptime\n", 7, &sendTimeout
) == -1)
101 if (ACE_OS::last_error() == ETIME
)
103 ACE_DEBUG ((LM_DEBUG
,
104 ACE_TEXT ("(%P|%t) Timeout while sending ")
105 ACE_TEXT ("query to status server\n")));
110 ACE_ERROR ((LM_ERROR
,
112 ACE_TEXT ("send_n")));
117 // Listing 7 code/ch06
119 ACE_Time_Value
recvTimeout (0, 1);
120 if ((bc
= status
.recv (buf
, sizeof(buf
), &recvTimeout
)) == -1)
122 ACE_ERROR ((LM_ERROR
,
128 log
.send_n (buf
, bc
);