#include
"
stdafx.h
"
#include
"
ace/OS_NS_time.h
"
#include
"
ace/Log_Msg.h
"
#include
"
ace/Reactor.h
"
#include
"
ace/Event_Handler.h
"
#include
"
ace/INET_Addr.h
"
#include
"
ace/SOCK_Dgram.h
"
#include
"
ace/TP_Reactor.h
"
#include
"
ace/WFMO_Reactor.h
"
class
ClientRecv :
public
ACE_Event_Handler
{
public
:
virtual
~
ClientRecv ()
{}
int
open (
const
ACE_INET_Addr
&
bind_addr);
//
Get this handler's I/O handle.
virtual
ACE_HANDLE get_handle (
void
)
const
{
return
this
->
udp.get_handle(); }
virtual
int
handle_input (ACE_HANDLE fd
=
ACE_INVALID_HANDLE);
//
Called when this handler is removed from the ACE_Reactor.
virtual
int
handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask);
ACE_SOCK_Dgram udp;
}
;
int
ClientRecv::open (
const
ACE_INET_Addr
&
bind_addr)
{
if
(
this
->
udp.open (bind_addr,
1
)
==
-
1
)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT (
"
%p\n
"
),
ACE_TEXT (
"
udp.open
"
)),
-
1
);
return
this
->
reactor ()
->
register_handler
(
this
, ACE_Event_Handler::READ_MASK );
}
int
ClientRecv::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask)
{
if
(close_mask
==
ACE_Event_Handler::WRITE_MASK)
return
0
;
close_mask
=
ACE_Event_Handler::ALL_EVENTS_MASK
|
ACE_Event_Handler::DONT_CALL;
this
->
reactor ()
->
remove_handler (
this
, close_mask);
this
->
udp.close();
delete
this
;
return
0
;
}
int
ClientRecv::handle_input (ACE_HANDLE fd )
{
char
buffer[
8192
]
=
{
0
}
;
ACE_INET_Addr peer_addr;
if
(
-
1
==
this
->
udp.recv(buffer,
8192
,peer_addr))
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT (
"
%p\n
"
),
ACE_TEXT (
"
udp.recv
"
)),
-
1
);
ACE_DEBUG((LM_INFO,
"
%s\n
"
,peer_addr.get_host_addr()));
return
0
;
}
DWORD WINAPI LoopThread(LPVOID lparam)
{
ACE_Reactor::instance ()
->
run_reactor_event_loop ();
return
0
;
}
int
_tmain(
int
argc, _TCHAR
*
argv[])
{
ACE_INET_Addr bind_addr(
10703
);
ClientRecv client;
client.reactor(ACE_Reactor::instance ());
if
(
-
1
==
client.open(bind_addr))
{
return
-
1
;
}
CreateThread(NULL,NULL,LoopThread,
0
,
0
,
0
);
ACE_INET_Addr send_addr;
for
(
int
i
=
1024
;i
<
3333
;i
++
)
{
ACE_DEBUG((LM_INFO,
"
%d \n
"
,i));
send_addr.
set
(i,
"
192.168.3.147
"
);
Sleep(
5000
);
if
(client.udp.send(
"
ttt
"
,
3
,send_addr)
==
-
1
)
{
ACE_DEBUG((LM_INFO,
"
%p
"
));
break
;
}
}
system(
"
pause
"
);
ACE_Reactor::instance ()
->
end_reactor_event_loop ();
return
0
;
}
报错:
udp.recv: connection reset
p?: handle is not a socket
莫非反应器只能用于TCP? , 没办法,先用WSAAsyncSelect吧。