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/OS_NS_errno.h"
9 #include "ace/Default_Constants.h"
12 /* BSD socket server. */
14 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
16 // Initialize WinSock DLL on Win32...
17 ACE_OS::socket_init (ACE_WSOCK_VERSION
);
20 ACE_HTONS (argc
> 1 ? ACE_OS::atoi (argv
[1]) : ACE_DEFAULT_SERVER_PORT
);
21 int sockbufsize
= argc
> 2 ? ACE_OS::atoi (argv
[2]) : 0;
22 struct sockaddr_in saddr
;
23 ACE_HANDLE s_handle
, n_handle
;
25 /* Create a local endpoint of communication */
26 if ((s_handle
= ACE_OS::socket (PF_INET
, SOCK_STREAM
, 0)) == ACE_INVALID_HANDLE
)
27 ACE_OS::perror (ACE_TEXT("socket")), ACE_OS::exit (1);
29 // If a sockbufsize was specified, set it for both send and receive.
32 if (ACE_OS::setsockopt (s_handle
, SOL_SOCKET
, SO_SNDBUF
,
33 (const char *) &sockbufsize
,
34 sizeof (sockbufsize
)) != 0)
35 ACE_OS::perror (ACE_TEXT("SO_SNDBUF")), ACE_OS::exit (1);
36 if (ACE_OS::setsockopt (s_handle
, SOL_SOCKET
, SO_RCVBUF
,
37 (const char *) &sockbufsize
,
38 sizeof (sockbufsize
)) != 0)
39 ACE_OS::perror (ACE_TEXT("SO_RCVBUF")), ACE_OS::exit (1);
42 /* Set up the address information to become a server */
43 ACE_OS::memset ((void *) &saddr
, 0, sizeof saddr
);
44 saddr
.sin_family
= AF_INET
;
45 saddr
.sin_port
= port_num
;
46 saddr
.sin_addr
.s_addr
= INADDR_ANY
;
48 /* Associate address with endpoint */
49 if (ACE_OS::bind (s_handle
,
50 reinterpret_cast<struct sockaddr
*> (&saddr
),
52 ACE_OS::perror (ACE_TEXT("bind")), ACE_OS::exit (1);
54 /* Make endpoint listen for service requests */
55 if (ACE_OS::listen (s_handle
, 5) == -1)
56 ACE_OS::perror (ACE_TEXT("listen")), ACE_OS::exit (1);
58 /* Performs the iterative server activities */
64 struct sockaddr_in cli_addr
;
65 int cli_addr_len
= sizeof cli_addr
;
68 /* Create a new endpoint of communication */
71 ACE_OS::accept (s_handle
,
72 reinterpret_cast<struct sockaddr
*> (&cli_addr
),
74 while (n_handle
== ACE_INVALID_HANDLE
&& errno
== EINTR
);
76 if (n_handle
== ACE_INVALID_HANDLE
)
78 ACE_OS::perror (ACE_TEXT("accept"));
82 int addr_len
= sizeof cli_addr
.sin_addr
.s_addr
;
83 hp
= ACE_OS::gethostbyaddr ((char *) &cli_addr
.sin_addr
,
87 ACE_OS::printf ("client %s\n", hp
->h_name
), ACE_OS::fflush (stdout
);
89 ACE_OS::perror (ACE_TEXT("gethostbyaddr"));
91 /* Read data from client (terminate on error) */
93 while ((r_bytes
= ACE_OS::recv (n_handle
, buf
, sizeof buf
)) > 0)
94 if (ACE_OS::write (ACE_STDOUT
, buf
, r_bytes
) != r_bytes
)
95 ACE_OS::perror (ACE_TEXT("write")), ACE_OS::exit (1);
97 if (ACE_OS::send (n_handle
, "", 1) != 1)
98 ACE_OS::perror ("write"), ACE_OS::exit (1);
100 /* Close the new endpoint
101 (listening endpoint remains open) */
102 if (ACE_OS::closesocket (n_handle
) == -1)
103 ACE_OS::perror (ACE_TEXT("close")), ACE_OS::exit (1);
107 ACE_NOTREACHED (return 0;)