Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Sockets / Server.cpp
blobcddb1b5f7c411e10e71ee59e596254cfae557a2a
1 #include "ace/os_include/os_netdb.h"
2 #include "ace/OS_NS_errno.h"
3 #include "ace/INET_Addr.h"
4 #include "ace/SOCK_Stream.h"
5 #include "ace/SOCK_Acceptor.h"
6 #include "ace/Log_Msg.h"
7 #include "ace/Time_Value.h"
9 int ACE_TMAIN (int, ACE_TCHAR *[])
11 // Listing 1 code/ch06
12 ACE_INET_Addr port_to_listen ("HAStatus");
13 ACE_SOCK_Acceptor acceptor;
15 if (acceptor.open (port_to_listen, 1) == -1)
16 ACE_ERROR_RETURN ((LM_ERROR,
17 ACE_TEXT ("%p\n"),
18 ACE_TEXT ("acceptor.open")),
19 100);
20 // Listing 1
22 //FUZZ: disable check_for_lack_ACE_OS
24 * The complete open signature:
26 // Listing 2 code/ch06
27 int open (const ACE_Addr &local_sap,
28 int reuse_addr = 0,
29 int protocol_family = PF_INET,
30 int backlog = ACE_DEFAULT_BACKLOG,
31 int protocol = 0);
32 // Listing 2
35 //FUZZ: enable check_for_lack_ACE_OS
37 while (1)
39 ACE_SOCK_Stream peer;
40 ACE_INET_Addr peer_addr;
41 ACE_Time_Value timeout (10, 0);
44 * Basic acceptor usage
46 #if 0
47 // Listing 3 code/ch06
48 if (acceptor.accept (peer) == -1)
49 ACE_ERROR_RETURN ((LM_ERROR,
50 ACE_TEXT ("(%P|%t) Failed to accept ")
51 ACE_TEXT ("client connection\n")),
52 100);
53 // Listing 3
54 #endif /* 0 */
56 // Listing 4 code/ch06
57 if (acceptor.accept (peer, &peer_addr, &timeout, 0) == -1)
59 if (ACE_OS::last_error() == EINTR)
60 ACE_DEBUG ((LM_DEBUG,
61 ACE_TEXT ("(%P|%t) Interrupted while ")
62 ACE_TEXT ("waiting for connection\n")));
63 else
64 if (ACE_OS::last_error() == ETIMEDOUT)
65 ACE_DEBUG ((LM_DEBUG,
66 ACE_TEXT ("(%P|%t) Timeout while ")
67 ACE_TEXT ("waiting for connection\n")));
69 // Listing 4
70 // Listing 5 code/ch06
71 else
73 ACE_TCHAR peer_name[MAXHOSTNAMELEN];
74 peer_addr.addr_to_string (peer_name, MAXHOSTNAMELEN);
75 ACE_DEBUG ((LM_DEBUG,
76 ACE_TEXT ("(%P|%t) Connection from %s\n"),
77 peer_name));
78 // Listing 5
79 // Listing 7 code/ch06
80 char buffer[4096];
81 ssize_t bytes_received;
83 while ((bytes_received =
84 peer.recv (buffer, sizeof(buffer))) != -1)
86 peer.send_n (buffer, bytes_received);
89 peer.close ();
90 // Listing 7
94 ACE_NOTREACHED (return 0);