Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / examples / IPC_SAP / SOCK_SAP / C-inclient.cpp
blobe4328d0f0b5fac93f1034da6492b26b3dee65e1c
1 #include "ace/OS_main.h"
2 #include "ace/OS_NS_string.h"
3 #include "ace/OS_NS_sys_socket.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "ace/OS_NS_stdlib.h"
6 #include "ace/OS_NS_stdio.h"
7 #include "ace/OS_NS_netdb.h"
8 #include "ace/Default_Constants.h"
11 /* BSD socket client */
13 int
14 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
16 // Initialize WinSock DLL on Win32...
17 ACE_OS::socket_init (ACE_WSOCK_VERSION);
19 struct sockaddr_in saddr;
20 struct hostent *hp;
21 const ACE_TCHAR *host = argc > 1 ? argv[1] : ACE_DEFAULT_SERVER_HOST;
22 u_short port_num =
23 ACE_HTONS (argc > 2 ? ACE_OS::atoi (argv[2]) : ACE_DEFAULT_SERVER_PORT);
24 int sockbufsize = argc > 3 ? ACE_OS::atoi (argv[3]) : 0;
25 char buf[BUFSIZ];
26 ACE_HANDLE s_handle;
27 int w_bytes;
28 int r_bytes;
29 int n;
31 // Create a local endpoint of communication.
32 if ((s_handle = ACE_OS::socket (PF_INET, SOCK_STREAM, 0)) == ACE_INVALID_HANDLE)
33 ACE_OS::perror (ACE_TEXT("socket")), ACE_OS::exit (1);
35 // If a sockbufsize was specified, set it for both send and receive.
36 if (sockbufsize > 0)
38 if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_SNDBUF,
39 (const char *) &sockbufsize,
40 sizeof (sockbufsize)) != 0)
41 ACE_OS::perror (ACE_TEXT("SO_SNDBUF")), ACE_OS::exit (1);
42 if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_RCVBUF,
43 (const char *) &sockbufsize,
44 sizeof (sockbufsize)) != 0)
45 ACE_OS::perror (ACE_TEXT("SO_RCVBUF")), ACE_OS::exit (1);
48 // Determine IP address of the server.
49 if ((hp = ACE_OS::gethostbyname (ACE_TEXT_ALWAYS_CHAR(host))) == 0)
50 ACE_OS::perror (ACE_TEXT("gethostbyname")), ACE_OS::exit (1);
52 // Set up the address information to contact the server.
53 ACE_OS::memset ((void *) &saddr, 0, sizeof saddr);
54 saddr.sin_family = AF_INET;
55 saddr.sin_port = port_num;
56 ACE_OS::memcpy (&saddr.sin_addr, hp->h_addr, hp->h_length);
58 // Establish connection with remote server.
59 if (ACE_OS::connect (s_handle,
60 reinterpret_cast<sockaddr *> (&saddr),
61 sizeof saddr) == -1)
62 ACE_OS::perror (ACE_TEXT("connect")), ACE_OS::exit (1);
64 // Send data to server (correctly handles "incomplete writes" due to
65 // flow control).
67 while ((r_bytes = ACE_OS::read (ACE_STDIN, buf, sizeof buf)) > 0)
68 for (w_bytes = 0; w_bytes < r_bytes; w_bytes += n)
69 if ((n = ACE_OS::send (s_handle, buf + w_bytes,
70 r_bytes - w_bytes)) < 0)
71 ACE_OS::perror (ACE_TEXT("write")), ACE_OS::exit (1);
73 if (ACE_OS::recv (s_handle, buf, 1) == 1)
74 ACE_OS::write (ACE_STDOUT, buf, 1);
76 // Explicitly close the connection.
77 if (ACE_OS::closesocket (s_handle) == -1)
78 ACE_OS::perror (ACE_TEXT("close")), ACE_OS::exit (1);
80 return 0;