1 // Simple multi-threaded database server example.
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/TLI_Acceptor.h"
5 #include "ace/Thread_Manager.h"
8 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_TLI)
10 // Global thread manager.
11 ACE_Thread_Manager thr_mgr
;
14 lookup_name (ACE_HANDLE handle
)
28 {123, "John Wayne Bobbit"},
30 {125, "O. J. Simpson"},
31 {126, "Bill Clinton"},
32 {127, "Rush Limbaugh"},
33 {128, "Michael Jackson"},
34 {129, "Kenneth Starr"},
36 {131, "Monica Lewinsky"},
45 ACE_TLI_Stream stream
;
46 char recvline
[MAXLINE
];
47 char sendline
[MAXLINE
];
50 "stream handle = %d, thread id = %t\n",
52 stream
.set_handle (handle
);
54 ssize_t n
= stream
.recv (recvline
, MAXLINE
, &flags
);
57 ACE_OS::t_error ("stream.recv error");
59 employee_id
= ACE_OS::atoi (recvline
);
62 for (index
= 0; found
== 0 && employee_db
[index
].emp_id
; index
++)
63 if (employee_id
== employee_db
[index
].emp_id
)
66 n
= ACE_OS::sprintf (sendline
,
67 "%s", employee_db
[index
].emp_name
);
71 n
= ACE_OS::sprintf (sendline
, "%s", "ERROR");
73 if (stream
.send (sendline
, n
+ 1, 0) == -1)
74 ACE_OS::t_error ("stream.send error");
76 if (stream
.sndrel () == -1)
77 ACE_OS::t_error ("stream.send error");
79 if (stream
.close () == -1)
80 ACE_OS::t_error ("stream.close error");
86 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
88 u_short port
= argc
> 1 ? ACE_OS::atoi (argv
[1]) : ACE_DEFAULT_SERVER_PORT
;
89 ACE_INET_Addr
l_addr (port
);
90 ACE_TLI_Acceptor
server (l_addr
, 1); // Create server, reuse addr if in use.
91 ACE_TLI_Stream new_stream
;
93 // Wait for a connection from a client. This is an example of a
98 if (server
.accept (new_stream
) == -1)
99 ACE_OS::t_error ("server.accept error");
101 if (thr_mgr
.spawn (ACE_THR_FUNC (lookup_name
),
102 (void *) new_stream
.get_handle (),
104 ACE_DEBUG ((LM_ERROR
,
105 "server: can't create worker thread %d\n"));
108 ACE_NOTREACHED (return 0);
112 int ACE_TMAIN (int, ACE_TCHAR
*[])
114 ACE_ERROR_RETURN ((LM_ERROR
,
115 "platform isn't configured to support TLI\n"),
118 #endif /* ACE_HAS_THREADS */