summaryrefslogtreecommitdiff
path: root/examples/IPC_SAP/SOCK_SAP/FD-unserver.cpp
blob: 378d50b739384a8718d9d14f7af57e8aea0008d8 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// $Id$

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

#if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)

// ACE_LSOCK Server

void
handle_client (ACE_LSOCK_Stream &stream)
{
  char buf[BUFSIZ];
  ACE_HANDLE handle;

  // Retrieve the socket descriptor passed from the client.

  if (stream.recv_handle (handle) == -1)
    ACE_ERROR ((LM_ERROR, "%p", "recv_handle"));
      
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) ----------------------------------------\n"));
      
  // Read data from client (correctly handles incomplete reads due to
  // flow control).
      
  for (ssize_t n; 
       (n = ACE_OS::read (handle, buf, sizeof buf)) > 0; 
       )
    ACE_DEBUG ((LM_DEBUG, "%*s", n, buf));
      
  ACE_OS::sprintf (buf, "%ld", ACE_OS::getpid ());
  
  ACE_DEBUG ((LM_DEBUG, "(%s, %d) ----------------------------------------\n", buf, ACE_OS::strlen (buf)));
      
  // Tell the client to shut down.
  if (stream.send_n (buf, ACE_OS::strlen (buf)) == -1)
    ACE_ERROR ((LM_ERROR, "%p", "send"));
      
  // Close new endpoint (listening endpoint stays open).
  if (stream.close () == -1) 
    ACE_ERROR ((LM_ERROR, "%p", "close"));
}

int 
main (int argc, char *argv[])
{                                                                
  const char *rendezvous = argc > 1 ? argv[1] : ACE_DEFAULT_RENDEZVOUS;
  // Create a server.
  ACE_OS::unlink (rendezvous);
  ACE_UNIX_Addr addr (rendezvous);
  ACE_LSOCK_Acceptor peer_acceptor (addr);
  ACE_LSOCK_Stream stream;
  
  // Performs the concurrent server activities.
  
  for (;;) 
    {
      // Create a new ACE_SOCK_Stream endpoint.
      if (peer_acceptor.accept (stream) == -1)
	ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "accept"), -1);

      ACE_DEBUG ((LM_DEBUG, "(%P|%t) accepted new connection\n"));
      
#if defined (VXWORKS)
      handle_client (stream);
#else
      switch (ACE_OS::fork (argv[0]))
	{
	case -1:
	  ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "fork"), -1);
	  /* NOTREACHED */
	case 0:
	  ACE_LOG_MSG->sync (argv[0]);
	  handle_client (stream);
	  ACE_OS::exit (0);
	  /* NOTREACHED */
	default:
	  stream.close ();
	}
#endif /* VXWORKS */
    }
  /* NOTREACHED */
  return 0;
}
#else
int
main (int, char *[])
{
  ACE_ERROR_RETURN ((LM_ERROR, "your platform doesn't not support UNIX domain sockets\n"), -1);
}
#endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */