1 #include "ace/LSOCK_Acceptor.h"
2 #include "ace/LSOCK_Stream.h"
3 #include "ace/UNIX_Addr.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_main.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_unistd.h"
9 #include "ace/OS_NS_stdlib.h"
12 #if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)
17 handle_client (ACE_LSOCK_Stream
&stream
)
22 // Retrieve the socket descriptor passed from the client.
24 if (stream
.recv_handle (handle
) == -1)
25 ACE_ERROR ((LM_ERROR
, "%p", "recv_handle"));
27 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) ----------------------------------------\n"));
29 // Read data from client (correctly handles incomplete reads due to
33 (n
= ACE_OS::read (handle
, buf
, sizeof buf
)) > 0;
35 ACE_DEBUG ((LM_DEBUG
, "%*s", n
, buf
));
37 ACE_OS::sprintf (buf
, "%d", static_cast<int> (ACE_OS::getpid ()));
39 ACE_DEBUG ((LM_DEBUG
, "(%s, %d) ----------------------------------------\n", buf
, ACE_OS::strlen (buf
)));
41 // Tell the client to shut down.
42 if (stream
.send_n (buf
, ACE_OS::strlen (buf
)) == -1)
43 ACE_ERROR ((LM_ERROR
, "%p", "send"));
45 // Close new endpoint (listening endpoint stays open).
46 if (stream
.close () == -1)
47 ACE_ERROR ((LM_ERROR
, "%p", "close"));
51 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
53 const ACE_TCHAR
*rendezvous
= argc
> 1 ? argv
[1] : ACE_DEFAULT_RENDEZVOUS
;
55 ACE_OS::unlink (rendezvous
);
56 ACE_UNIX_Addr
addr (rendezvous
);
57 ACE_LSOCK_Acceptor
peer_acceptor (addr
);
58 ACE_LSOCK_Stream stream
;
60 // Performs the concurrent server activities.
64 // Create a new ACE_SOCK_Stream endpoint.
65 if (peer_acceptor
.accept (stream
) == -1)
66 ACE_ERROR_RETURN ((LM_ERROR
, "(%P|%t) %p\n", "accept"), -1);
68 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) accepted new connection\n"));
71 handle_client (stream
);
73 switch (ACE_OS::fork (argv
[0]))
76 ACE_ERROR_RETURN ((LM_ERROR
, "(%P|%t) %p\n", "fork"), -1);
79 ACE_LOG_MSG
->sync (argv
[0]);
80 handle_client (stream
);
88 ACE_NOTREACHED (return 0;)
92 ACE_TMAIN (int, ACE_TCHAR
*[])
94 ACE_ERROR_RETURN ((LM_ERROR
, "your platform doesn't not support UNIX domain sockets\n"), -1);
96 #endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */