Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Sockets / Basic_Robust.cpp
blob6626cebc09d537e8607bcfe6a86f4a8c26067d8c
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
18 ACE_INET_Addr addr;
19 ...
20 addr.set ("HAStatus", ACE_LOCALHOST);
21 ...
22 addr.set ("HALog", ACE_LOCALHOST);
23 // Listing 1
27 ACE_INET_Addr addr;
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)
35 ACE_DEBUG ((LM_DEBUG,
36 ACE_TEXT ("(%P|%t) Connecting to %s\n"),
37 peerAddress));
39 // Listing 2
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,
47 ACE_TEXT ("%p\n"),
48 ACE_TEXT ("status")), 100);
49 // Listing 3
51 addr.set (ACE_TEXT("HALog"), ACE_LOCALHOST);
52 if (addr.addr_to_string (peerAddress,
53 sizeof(peerAddress), 0) == 0)
55 ACE_DEBUG ((LM_DEBUG,
56 ACE_TEXT ("(%P|%t) Connecting to %s\n"),
57 peerAddress ));
60 // Listing 4 code/ch06
61 ACE_SOCK_Connector logConnector;
62 ACE_Time_Value timeout (10);
63 ACE_SOCK_Stream log;
64 if (logConnector.connect (log, addr, &timeout) == -1)
66 if (ACE_OS::last_error() == ETIME)
68 ACE_DEBUG ((LM_DEBUG,
69 ACE_TEXT ("(%P|%t) Timeout while ")
70 ACE_TEXT ("connecting to log server\n")));
72 else
74 ACE_ERROR ((LM_ERROR,
75 ACE_TEXT ("%p\n"),
76 ACE_TEXT ("log")));
78 return (101);
80 // Listing 4
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)
90 ...
91 // Listing 5
95 char buf[64];
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")));
107 // Listing 6
108 else
110 ACE_ERROR ((LM_ERROR,
111 ACE_TEXT ("%p\n"),
112 ACE_TEXT ("send_n")));
114 return (102);
117 // Listing 7 code/ch06
118 ssize_t bc ;
119 ACE_Time_Value recvTimeout (0, 1);
120 if ((bc = status.recv (buf, sizeof(buf), &recvTimeout)) == -1)
122 ACE_ERROR ((LM_ERROR,
123 ACE_TEXT ("%p\n"),
124 ACE_TEXT ("recv")));
125 return (103);
128 log.send_n (buf, bc);
129 // Listing 7
131 status.close ();
132 log.close ();
134 return (0);