Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / examples / IPC_SAP / TLI_SAP / CPP-client.cpp
blob6e3e63413da99f554f337855bb9400491ef50f67
1 #include "ace/TLI_Connector.h"
2 #include "ace/INET_Addr.h"
3 #include "ace/Log_Msg.h"
4 #include "ace/OS_NS_stdlib.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/Time_Value.h"
9 #if defined (ACE_HAS_TLI)
11 /* ACE_TLI Client */
13 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
15 const ACE_TCHAR *host = argc > 1 ? argv[1] : ACE_DEFAULT_SERVER_HOST;
16 u_short r_port = argc > 2 ? ACE_OS::atoi (argv[2]) : ACE_DEFAULT_SERVER_PORT;
17 int timeout = argc > 3 ? ACE_OS::atoi (argv[3]) : ACE_DEFAULT_TIMEOUT;
18 u_short l_port = argc > 4 ? ACE_OS::atoi (argv[4]) : ACE_DEFAULT_LOCAL_PORT;
20 char buf[BUFSIZ];
22 ACE_TLI_Stream cli_stream;
24 ACE_INET_Addr remote_addr (r_port, host);
25 ACE_INET_Addr local_addr (l_port);
27 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("starting non-blocking connect\n")));
29 // Initiate timed, non-blocking connection with server.
30 ACE_TLI_Connector con;
32 if (con.connect (cli_stream,
33 remote_addr,
34 (ACE_Time_Value *) &ACE_Time_Value::zero,
35 local_addr,
36 1) == -1)
38 if (errno != EWOULDBLOCK)
39 ACE_ERROR_RETURN ((LM_ERROR,
40 ACE_TEXT ("%p\n"),
41 ACE_TEXT ("connection failed")),
42 1);
44 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("starting timed connect\n")));
46 // Check if non-blocking connection is in progress, and wait up
47 // to timeout seconds for it to complete.
48 ACE_Time_Value tv (timeout);
50 if (con.complete (cli_stream,
51 &remote_addr,
52 &tv) == -1)
53 ACE_ERROR_RETURN ((LM_ERROR,
54 ACE_TEXT ("%p\n"),
55 ACE_TEXT ("connection failed")),
56 1);
57 else
58 ACE_DEBUG ((LM_DEBUG,
59 ACE_TEXT ("connected to %s\n"),
60 remote_addr.get_host_name ()));
63 // Send data to server (correctly handles "incomplete writes").
65 for (int r_bytes;
66 (r_bytes = ACE_OS::read (ACE_STDIN, buf, sizeof buf)) > 0;
68 if (cli_stream.send_n (buf,
69 r_bytes,
70 0) == -1)
71 ACE_ERROR_RETURN ((LM_ERROR,
72 ACE_TEXT ("%p\n"),
73 ACE_TEXT ("send_n")),
74 1);
76 // Explicitly close the connection.
77 if (cli_stream.close () == -1)
78 ACE_ERROR_RETURN ((LM_ERROR,
79 ACE_TEXT ("%p\n"),
80 ACE_TEXT ("close")),
81 -1);
82 return 0;
84 #else
85 int ACE_TMAIN (int, ACE_TCHAR *[])
87 ACE_ERROR_RETURN ((LM_ERROR,
88 ACE_TEXT ("your platform isn't configured to support TLI\n")),
89 1);
91 #endif /* ACE_HAS_TLI */