2 * server for a basic connection establishment test using HTBP
5 #include "ace/Log_Msg.h"
7 #include "ace/HTBP/HTBP_Session.h"
8 #include "ace/HTBP/HTBP_Stream.h"
9 #include "ace/HTBP/HTBP_Addr.h"
11 #include "ace/SOCK_Acceptor.h"
12 #include "ace/SOCK_Stream.h"
13 #include "ace/Get_Opt.h"
14 #include "ace/OS_NS_stdio.h"
15 #include "ace/OS_NS_sys_socket.h"
16 #include "ace/os_include/os_netdb.h"
19 const ACE_TCHAR
*notifier_file
= 0;
21 parse_args (int argc
, ACE_TCHAR
*argv
[])
23 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("p:o:"));
26 while ((c
= get_opts ()) != -1)
30 notifier_file
= get_opts
.opt_arg();
33 port
= static_cast<unsigned>(ACE_OS::atoi (get_opts
.opt_arg()));
37 ACE_ERROR_RETURN ((LM_ERROR
,
44 // Indicates successful parsing of the command line
49 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
53 ACE_OS::socket_init (ACE_WSOCK_VERSION
);
55 if (parse_args(argc
, argv
) != 0)
58 ACE_TCHAR host
[MAXHOSTNAMELEN
+1];
59 if (ACE_OS::hostname (host
, MAXHOSTNAMELEN
) != 0)
60 ACE_ERROR_RETURN ((LM_ERROR
,
61 ACE_TEXT ("(%P|%t) Server failure: %p\n"),
62 ACE_TEXT ("hostname")),
65 ACE_INET_Addr
local (port
, host
);
66 ACE_SOCK_Stream sock
[2];
67 ACE_SOCK_Acceptor
acc(local
, 1);
68 local
.addr_to_string (host
, MAXHOSTNAMELEN
);
70 ACE_TEXT ("(%P|%t) Server is ready on %s\n"),
72 if (notifier_file
!= 0)
74 FILE *f
= ACE_OS::fopen (notifier_file
,ACE_TEXT("w+"));
75 const char *msg
= "server ready";
76 ACE_OS::fwrite (msg
,ACE_OS::strlen(msg
),1,f
);
80 ACE::HTBP::Channel
channel1(sock
[0]);
82 ACE_TEXT ("(%P|%t) Server: ")
83 ACE_TEXT ("Got sock[0], handle = %d\n"),
84 sock
[0].get_handle()));
86 ACE::HTBP::Channel
channel2 (sock
[1]);
88 ACE_TEXT ("(%P|%t) Server: ")
89 ACE_TEXT ("Got sock[1], handle = %d\n"),
90 sock
[1].get_handle()));
92 while ((res
= channel1
.pre_recv ()) != 0)
95 ACE_TEXT ("(%P|%t) Server: ")
96 ACE_TEXT ("res = %d. waiting 1 sec. %p\n"),
98 ACE_TEXT ("stream.pre_recv()")));
102 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Read from channel2\n")));
103 while ((res
= channel2
.pre_recv()) != 0)
105 ACE_DEBUG ((LM_DEBUG
,
106 ACE_TEXT ("(%P|%t) Server: ")
107 ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
109 ACE_TEXT ("stream2.pre_recv()")));
113 ACE::HTBP::Session
*session
= channel1
.session();
114 ACE::HTBP::Stream
stream (session
);
116 ACE_DEBUG ((LM_DEBUG
,
117 ACE_TEXT ("(%P|%t) Server: ")
118 ACE_TEXT ("using streams %d, %d. Got session = %@\n"),
119 sock
[0].get_handle(),
120 sock
[1].get_handle(),
123 for (int i
= 0; i
>= 0; i
++)
126 while ((n
= stream
.recv(buffer
,1000)) == -1
127 && (errno
== EWOULDBLOCK
|| errno
== ETIME
)
131 ACE_DEBUG ((LM_DEBUG
,
132 ACE_TEXT ("(%P|%t) Server: ")
133 ACE_TEXT ("waiting for inbound data, %d tries left\n"),
137 if (retrycount
== 0 || n
< 0)
142 ACE_DEBUG ((LM_DEBUG
,
143 ACE_TEXT ("(%P|%t) Server: ")
144 ACE_TEXT ("Got: \"%C\"\n"), buffer
));
146 if (ACE_OS::strstr (buffer
,"goodbye") != 0)
149 ACE_OS::sprintf (buffer
,"I hear you %d",i
);
150 n
= stream
.send (buffer
,ACE_OS::strlen(buffer
)+1);
152 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
153 ACE_TEXT ("stream.send")),
156 ACE_DEBUG ((LM_DEBUG
,
157 ACE_TEXT ("(%P|%t) Server: Send returned %d\n"), n
));
159 int got
[2] = {-1,-1};
160 while (got
[0] == -1 || got
[1] == -1)
164 if ((got
[0] = (res
=channel1
.pre_recv())) == -1)
165 ACE_DEBUG ((LM_DEBUG
,
166 ACE_TEXT ("(%P|%t) Server: ")
167 ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
169 ACE_TEXT ("channel1.pre_recv()")));
173 if ((got
[1] = (res
=channel2
.pre_recv())) == -1)
174 ACE_DEBUG ((LM_DEBUG
,
175 ACE_TEXT ("(%P|%t) Server: ")
176 ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
178 ACE_TEXT ("channel2.pre_recv()")));
180 if (got
[0] == -1 || got
[1] == -1)
184 ACE_OS::sleep(1); // prevent test failure on windows when the connection