|
我写了一个简单的程序,就是accept一个连接,然后从这个连接收数据。程序如下所示。现在的问题是,当有新的连接连上时,accept是正确的,但是确不能收到数据,也就是handle_read_stream总是不能被回调。请行家帮我看看怎么回事,多谢多谢。-
- #include "ace/ACE.h"
- #include "ace/SOCK_Stream.h"
- #include "ace/SOCK_Acceptor.h"
- #include "ace/Proactor.h"
- #include "ace/Asynch_IO.h"
- #include "ace/Message_Block.h"
- #include <typeinfo.h>
- u_short g_listen_port = 9990;
- class TestHandler : public ACE_Handler
- {
- public:
- void startup(ACE_Proactor *init_proactor)
- {
- m_proactor = init_proactor;
- if (m_acceptor.open(ACE_INET_Addr(g_listen_port)) == -1)
- {
- printf("can not open acceptor\n");
- }
- if (m_asynch_accept.open(*this,
- m_acceptor.get_handle(), 0, m_proactor) == -1)
- {
- printf("can not open asynch acceptor\n");
- }
- m_rd_mb = new ACE_Message_Block(500);
- m_asynch_accept.accept(*m_rd_mb, 0);
- }
- protected:
- virtual void handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
- {
- printf("?\n");
- if (result.success () && result.bytes_transferred () != 0)
- {
- printf("recv bytes %u\n", result.bytes_transferred());
- m_asynch_rd_strm.read(*m_rd_mb, 6);
- }
- }
- virtual void handle_accept (const ACE_Asynch_Accept::Result &result)
- {
- if (result.success())
- {
- printf("accept a connection\n");
- printf("\tbytes_to_read %u\n", result.bytes_to_read());
- if (m_asynch_rd_strm.open(*this, result.accept_handle()) == -1)
- {
- printf("can not open asynchronous stream\n");
- }
- printf("\taccept handle 0x%08x\n", (int)result.accept_handle());
- m_asynch_rd_strm.read(*m_rd_mb, 6);
- }
- }
- private:
- ACE_Asynch_Accept m_asynch_accept;
- ACE_Asynch_Read_Stream m_asynch_rd_strm;
- ACE_SOCK_Acceptor m_acceptor;
- ACE_Proactor *m_proactor;
- ACE_Message_Block *m_rd_mb;
- };
- int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
- {
- ACE::init();
- ACE_Proactor my_proactor;
- printf("%s\n", typeid(*my_proactor.implementation()).name());
- TestHandler test_handler;
- test_handler.startup(&my_proactor);
- my_proactor.proactor_run_event_loop();
- ACE::fini();
- return 0;
- }
复制代码 |
|