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
|
#include "ace/OS.h"
// $Id$
/* BSD socket server. */
int main (int argc, char *argv[])
{
// Initialize WinSock DLL on Win32...
ACE_OS::socket_init (ACE_WSOCK_VERSION);
u_short port_num =
htons (argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT);
struct sockaddr_in saddr;
ACE_HANDLE s_handle, n_handle;
/* Create a local endpoint of communication */
if ((s_handle = ACE_OS::socket (PF_INET, SOCK_STREAM, 0)) == ACE_INVALID_HANDLE)
ACE_OS::perror ("socket"), ACE_OS::exit (1);
/* Set up the address information to become a server */
ACE_OS::memset ((void *) &saddr, 0, sizeof saddr);
saddr.sin_family = AF_INET;
saddr.sin_port = port_num;
saddr.sin_addr.s_addr = INADDR_ANY;
/* Associate address with endpoint */
if (ACE_OS::bind (s_handle, (struct sockaddr *) &saddr,
sizeof saddr) == -1)
ACE_OS::perror ("bind"), ACE_OS::exit (1);
/* Make endpoint listen for service requests */
if (ACE_OS::listen (s_handle, 5) == -1)
ACE_OS::perror ("listen"), ACE_OS::exit (1);
/* Performs the iterative server activities */
for (;;)
{
char buf[BUFSIZ];
int r_bytes;
struct sockaddr_in cli_addr;
int cli_addr_len = sizeof cli_addr;
struct hostent *hp;
/* Create a new endpoint of communication */
do
n_handle = ACE_OS::accept (s_handle, (struct sockaddr *)
&cli_addr, &cli_addr_len);
while (n_handle == ACE_INVALID_HANDLE && errno == EINTR);
if (n_handle == ACE_INVALID_HANDLE)
{
ACE_OS::perror ("accept");
continue;
}
int addr_len = sizeof cli_addr.sin_addr.s_addr;
hp = ACE_OS::gethostbyaddr ((char *) &cli_addr.sin_addr,
addr_len, AF_INET);
if (hp != 0)
ACE_OS::printf ("client %s\n", hp->h_name), ACE_OS::fflush (stdout);
else
ACE_OS::perror ("gethostbyaddr");
/* Read data from client (terminate on error) */
while ((r_bytes = ACE_OS::recv (n_handle, buf, sizeof buf)) > 0)
if (ACE_OS::write (ACE_STDOUT, buf, r_bytes) != r_bytes)
ACE_OS::perror ("write"), ACE_OS::exit (1);
if (ACE_OS::send (n_handle, "", 1) != 1)
::perror ("write"), ACE_OS::exit (1);
/* Close the new endpoint
(listening endpoint remains open) */
if (ACE_OS::closesocket (n_handle) == -1)
ACE_OS::perror ("close"), ACE_OS::exit (1);
ACE_OS::exit (0);
}
/* NOTREACHED */
return 0;
}
|