summaryrefslogtreecommitdiff
path: root/examples/IPC_SAP/SOCK_SAP/CPP-unclient.cpp
blob: 09bd2ad7e12417a2293577269abf5ae3df7e5bbb (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
// $Id$

// ACE_LSOCK Client.

#include "ace/LSOCK_Connector.h"
#include "ace/UNIX_Addr.h"                              
                                                        
ACE_RCSID(SOCK_SAP, CPP_unclient, "$Id$")

#if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)

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

  ACE_LSOCK_Stream cli_stream;
  ACE_LSOCK_Connector con;
  ACE_UNIX_Addr remote_addr (rendezvous);
                                                        
  // Establish the connection with server.
  if (con.connect (cli_stream, remote_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "connect"),
                      1);

  // Send data to server (correctly handles "incomplete writes").
  
  for (int r_bytes; 
       (r_bytes = ACE_OS::read (ACE_STDIN, buf, sizeof buf)) > 0; 
       )
    if (cli_stream.send_n (buf, r_bytes) == -1) 
      ACE_ERROR_RETURN ((LM_ERROR,
                         "%p\n",
                         "send_n"),
                        1);

  // Explicitly close the writer-side of the connection.
  if (cli_stream.close_writer () == -1) 
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close_writer"),
                      1);

  // Wait for handshake with server.
  if (cli_stream.recv_n (buf, 1) != 1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "recv_n"),
                      1);    

  // Close the connection completely. 
  if (cli_stream.close () == -1) 
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close"),
                      1);
  return 0;
}                                                       
#else
int main (int, char *[])
{
  ACE_ERROR_RETURN ((LM_ERROR, 
		     "this platform does not support UNIX-domain sockets\n"), -1);
}
#endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */