找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3696|回复: 2

初学practor的问题,不能收数据

[复制链接]
发表于 2008-7-15 22:09:51 | 显示全部楼层 |阅读模式
我写了一个简单的程序,就是accept一个连接,然后从这个连接收数据。程序如下所示。现在的问题是,当有新的连接连上时,accept是正确的,但是确不能收到数据,也就是handle_read_stream总是不能被回调。请行家帮我看看怎么回事,多谢多谢。
  1. #include "ace/ACE.h"
  2. #include "ace/SOCK_Stream.h"
  3. #include "ace/SOCK_Acceptor.h"
  4. #include "ace/Proactor.h"
  5. #include "ace/Asynch_IO.h"
  6. #include "ace/Message_Block.h"
  7. #include <typeinfo.h>
  8. u_short g_listen_port = 9990;
  9. class TestHandler : public ACE_Handler
  10. {
  11. public:
  12. void startup(ACE_Proactor *init_proactor)
  13. {
  14.   m_proactor = init_proactor;
  15.   if (m_acceptor.open(ACE_INET_Addr(g_listen_port)) == -1)
  16.   {
  17.    printf("can not open acceptor\n");
  18.   }
  19.   if (m_asynch_accept.open(*this,
  20.    m_acceptor.get_handle(), 0, m_proactor) == -1)
  21.   {
  22.    printf("can not open asynch acceptor\n");
  23.   }
  24.   m_rd_mb = new ACE_Message_Block(500);
  25.   m_asynch_accept.accept(*m_rd_mb, 0);
  26. }
  27. protected:
  28. virtual void handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
  29. {
  30.   printf("?\n");
  31.   if (result.success () && result.bytes_transferred () != 0)
  32.   {
  33.    printf("recv bytes %u\n", result.bytes_transferred());
  34.    m_asynch_rd_strm.read(*m_rd_mb, 6);
  35.   }
  36. }
  37. virtual void handle_accept (const ACE_Asynch_Accept::Result &result)
  38. {
  39.   if (result.success())
  40.   {
  41.    printf("accept a connection\n");
  42.    printf("\tbytes_to_read %u\n", result.bytes_to_read());
  43.    if (m_asynch_rd_strm.open(*this, result.accept_handle()) == -1)
  44.    {
  45.     printf("can not open asynchronous stream\n");
  46.    }
  47.    printf("\taccept handle 0x%08x\n", (int)result.accept_handle());
  48.    m_asynch_rd_strm.read(*m_rd_mb, 6);
  49.   }
  50. }
  51. private:
  52. ACE_Asynch_Accept m_asynch_accept;
  53. ACE_Asynch_Read_Stream m_asynch_rd_strm;
  54. ACE_SOCK_Acceptor m_acceptor;
  55. ACE_Proactor *m_proactor;
  56. ACE_Message_Block *m_rd_mb;
  57. };
  58. int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
  59. {
  60. ACE::init();
  61. ACE_Proactor my_proactor;
  62. printf("%s\n", typeid(*my_proactor.implementation()).name());
  63. TestHandler test_handler;
  64. test_handler.startup(&my_proactor);
  65. my_proactor.proactor_run_event_loop();
  66. ACE::fini();
  67. return 0;
  68. }
复制代码
 楼主| 发表于 2008-7-15 22:10:01 | 显示全部楼层
需要你自己主动的投递一次异步的读操作,光等是没用的。
 楼主| 发表于 2008-7-15 22:10:12 | 显示全部楼层
推荐你看一下ACE的实例程序,里面有如何使用。
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-9 10:21 , Processed in 0.013977 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表