summaryrefslogtreecommitdiff
path: root/examples/IPC_SAP/SOCK_SAP/CPP-unserver.cpp
blob: 25ecf7aa8cd2ae34a5305f284a00290869e8df44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* ACE_LSOCK Server */
// $Id$



#include "ace/LSOCK_Acceptor.h"                             
#include "ace/LSOCK_Stream.h"
#include "ace/UNIX_Addr.h"                                       

#if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)

int 
main (int argc, char *argv[])
{                                                                
  char *rendezvous = argc > 1 ? argv[1] : ACE_DEFAULT_RENDEZVOUS;

  /* Create a server address. */
  ACE_UNIX_Addr server_addr (rendezvous);

  ACE_LSOCK_Acceptor peer_acceptor;

  /* Create a server */

  if (peer_acceptor.open (server_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), 1);

  /* Keep these guys out here to prevent excessive constructor
     calls... */
  ACE_LSOCK_Stream new_stream;                                   
  ACE_UNIX_Addr    cli_addr;

  ACE_DEBUG ((LM_DEBUG, "starting server %s\n", 
	     server_addr.get_path_name ()));

  /* Performs the iterative server activities */

  for (;;) 
    {
      char buf[BUFSIZ];                                          
      ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
                                                                     
      /* Create a new ACE_SOCK_Stream endpoint (note
         automatic restart if errno == EINTR) */

      if (peer_acceptor.accept (new_stream, &cli_addr, &timeout) == -1)
	{
	  ACE_ERROR ((LM_ERROR, "%p\n", "accept"));
	  continue;
	}          
                                                                 
      ACE_DEBUG ((LM_DEBUG, "client %s\n", 
		 cli_addr.get_path_name ()));

      /* Read data from client (terminate on error) */

      for (int r_bytes; 
	   (r_bytes = new_stream.recv (buf, sizeof buf)) > 0; )
        if (ACE_OS::write (ACE_STDOUT, buf, r_bytes) != r_bytes)
	  ACE_ERROR ((LM_ERROR, "%p\n", "ACE::send_n"));

      if (new_stream.send_n ("", 1) != 1)
	ACE_ERROR ((LM_ERROR, "%p\n", "send_n"));

      /* Close new endpoint (listening endpoint stays open) */
      if (new_stream.close () == -1) 
	ACE_ERROR ((LM_ERROR, "%p\n", "close"));
    }

  /* NOTREACHED */
  return 0;
}
#else
int main (void)
{
  ACE_ERROR_RETURN ((LM_ERROR, 
		     "this platform does not support UNIX-domain sockets\n"), -1);
}
#endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */