1 // This example tests the features of the ACE_LSOCK_Acceptor and
2 // ACE_LSOCK_Stream classes. If the platform supports threads it uses
3 // a thread-per-request concurrency model.
5 #include "ace/LSOCK_Acceptor.h"
6 #include "ace/Thread_Manager.h"
7 #include "ace/OS_main.h"
8 #include "ace/OS_NS_unistd.h"
11 #if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)
13 // Are we running verbosely?
14 static int verbose
= 1;
16 // Entry point into the server task.
21 ACE_UNIX_Addr cli_addr
;
22 ACE_LSOCK_Stream new_stream
;
23 ACE_HANDLE handle
= (ACE_HANDLE
) (long) arg
;
25 new_stream
.set_handle (handle
);
27 // Make sure we're not in non-blocking mode.
28 if (new_stream
.disable (ACE_NONBLOCK
) == -1)
29 ACE_ERROR_RETURN ((LM_ERROR
,
31 ACE_TEXT ("disable")),
34 if (new_stream
.get_remote_addr (cli_addr
) == -1)
37 ACE_TEXT ("get_remote_addr")));
40 ACE_TEXT ("(%P|%t) client connected from %C\n"),
41 cli_addr
.get_path_name ()));
43 // Read data from client (terminate on error).
49 ssize_t r_bytes
= new_stream
.recv (buf
, sizeof buf
);
58 else if (r_bytes
== 0)
61 ACE_TEXT ("(%P|%t) reached end of input, connection closed by client\n")));
64 else if (verbose
&& ACE::write_n (ACE_STDOUT
, buf
, r_bytes
) != r_bytes
)
67 ACE_TEXT ("ACE::write_n")));
68 else if (new_stream
.send_n (buf
, r_bytes
) != r_bytes
)
71 ACE_TEXT ("send_n")));
74 // Close new endpoint (listening endpoint stays open).
75 if (new_stream
.close () == -1)
84 run_event_loop (const ACE_TCHAR rendezvous
[])
86 ACE_LSOCK_Acceptor peer_acceptor
;
88 // Create a server address.
89 ACE_UNIX_Addr
server_addr (rendezvous
);
91 ACE_OS::unlink (rendezvous
);
95 if (peer_acceptor
.open (server_addr
) == -1)
96 ACE_ERROR_RETURN ((LM_ERROR
,
100 else if (peer_acceptor
.get_local_addr (server_addr
) == -1)
101 ACE_ERROR_RETURN ((LM_ERROR
,
103 ACE_TEXT ("get_local_addr")),
106 ACE_DEBUG ((LM_DEBUG
,
107 ACE_TEXT ("starting server %C\n"),
108 server_addr
.get_path_name ()));
110 // Keep these guys out here to prevent excessive constructor
112 ACE_LSOCK_Stream new_stream
;
114 // Performs the iterative server activities.
118 ACE_Time_Value
timeout (ACE_DEFAULT_TIMEOUT
);
120 if (peer_acceptor
.accept (new_stream
, 0, &timeout
) == -1)
122 ACE_ERROR ((LM_ERROR
,
124 ACE_TEXT ("accept")));
128 #if defined (ACE_HAS_THREADS)
129 if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC
) server
,
130 reinterpret_cast<void *> (new_stream
.get_handle ()),
132 ACE_ERROR_RETURN ((LM_ERROR
,
133 ACE_TEXT ("(%P|%t) %p\n"),
137 server (reinterpret_cast<void *> (new_stream
.get_handle ()));
138 #endif /* ACE_HAS_THREADS */
141 ACE_NOTREACHED (return 0;)
145 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
147 return run_event_loop (argc
> 1 ? argv
[1] : ACE_DEFAULT_RENDEZVOUS
);
150 int ACE_TMAIN (int, ACE_TCHAR
*[])
152 ACE_ERROR_RETURN ((LM_ERROR
,
153 "this platform does not support UNIX-domain sockets\n"), -1);
155 #endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */