ACE_Proactor模式客户端连接服务器的问题
客户端和服务器端都采用ACE_Proactor模式编写。不管服务器端是否启动,客户端连接时 if (connector.connect(addr) == -1) 判断都能通过。
启动服务器时,连接正常服务器端也能收发数据。
为什么不启动服器时if (connector.connect(addr) == -1) 判断也能通过。
ACE::init();
ACE_INET_Addr addr(60401,"127.0.0.1");
HA_Proactive_Service *client = new HA_Proactive_Service();
ACE_Asynch_Connector<HA_Proactive_Service> connector;
connector.open();
if (connector.connect(addr) == -1)
return -1; // 不管服务器是否启动,该语句都不能执行
while(true)
ACE_Proactor::instance ()->handle_events ();
ACE::fini();
return 0; class HA_Proactive_Service : public ACE_Service_Handler
{
public:
~HA_Proactive_Service ()
{
if (this->handle () != ACE_INVALID_HANDLE)
ACE_OS::closesocket (this->handle ());
}
virtual void open (ACE_HANDLE h, ACE_Message_Block&)
{
this->handle (h);
if (this->writer_.open (*this) != 0 )
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
ACE_TEXT ("HA_Proactive_Service open")));
delete this;
return;
}
ACE_OS::printf("connceted");
for(int i=0;i<10;i++)
{
ACE_OS::sleep(1);
time_t now = ACE_OS::gettimeofday().sec();
char *time = ctime(&now); //获取当前时间的字符串格式
ACE_Message_Block *mb = new ACE_Message_Block(100);
mb->copy(time);
if (this->writer_.write(*mb,mb->length()) !=0)
{
ACE_OS::printf("Begin read fail\n");
delete this;
return;
}
}
return;
}
//异步写完成后会调用此函数
virtual void handle_write_dgram
(const ACE_Asynch_Write_Stream::Result &result)
{
ACE_Message_Block &mb = result.message_block ();
mb.release();
return;
}
private:
ACE_Asynch_Write_Stream writer_;
}; 因为这是Proactor,connet()不会返回结果。结果要在异步调用validate_connect()获取 因为它是ACE_ Asynch_Connector
楼主还是没明白异步的具体含义。。。 从基础学起吧.呵呵.
页:
[1]