psycheqiqi 发表于 2009-10-15 15:16:05

proactor的address方法

我想从服务器端获取连接的客户端的地址,看了很多有一个ADDRESS方法

在HA_Proactive_Service类中重载address方法
virtual void address
(const ACE_INET_Addr &remote_address, const ACE_INET_Addr &local_address)
{
   cout << remote_address.get_ip_address() << endl;
}
但address方法中的那2个参数怎么设置。服务器端只有一个地址
ACE_INET_Addr listen_addr(3000);
ACE_Asynch_Acceptor<HA_Proactive_Service>aio_acceptor;
if (0 != aio_acceptor.open(listen_addr, 0, 0, 5, 1, 0, 0))

有完整的例子吗?

modern 发表于 2009-10-15 16:36:49

address是回调函数,这两个参数是由Proactor框架传给你的。

psycheqiqi 发表于 2009-10-16 09:07:07

回复 #2 modern 的帖子

但我如何调用address函数,还是系统调用的。还是不明白如何获得客户端的地址

服务器端代码:
HA_Proactive_Service类
        virtual void address
                (const ACE_INET_Addr &remote_address, const ACE_INET_Addr &local_address)
        {
                CPserverDlg *dlg=(CPserverDlg*)AfxGetApp()->GetMainWnd();
                CString str;
                str.Format("地址:%d",remote_address.get_ip_address());
                dlg->MessageBox(str);
//                cout << remote_address.get_ip_address() << endl;

        }
       
        virtual void open (ACE_HANDLE h, ACE_Message_Block&)
        {
//                _cur = this;
//                num++;
                _cur=this;
                List.push_back(_cur);
                this->handle (h) ;
                if (this->writer_.open (*this) != 0 ||this->reader_.open(*this) !=0)
                {
                        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
                                ACE_TEXT ("HA_Proactive_Service open")));
                        delete this;
                        return;
                }
                ACE_Message_Block *mb;
                ACE_NEW_NORETURN(mb,ACE_Message_Block(8102));
               
                if (this->reader_.read(*mb, mb->space ()) != 0)
                {
                        ACE_OS::printf("Begin read fail\n");
                        delete this;
                        mb->release();
                        return;
                }
      return;               

        }
对话框.cpp

UINT ThreadReactor(LPVOID param)
{
    ACE_INET_Addr listen_addr(3000);
        ACE_Asynch_Acceptor<HA_Proactive_Service>aio_acceptor;
        if (0 != aio_acceptor.open(listen_addr, 0, 0, 5, 1, 0, 0))
                ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("acceptor open")), 1);
        CPserverDlg *dlg=(CPserverDlg*)AfxGetApp()->GetMainWnd();
        dlg->MessageBox("服务器已启动");
    int result = ACE_Proactor::instance ()->proactor_run_event_loop ();
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT("Thread ended\n")));
    return result;
}

void CPserverDlg::OnStart()
{
        // TODO: Add your control notification handler code here
        UpdateData(TRUE);
        AfxBeginThread(ThreadReactor,NULL);
        UpdateData(FALSE);       
}

void CPserverDlg::OnStop()
{
        // TODO: Add your control notification handler code here
    ACE_Proactor::instance()->proactor_end_event_loop();
       
}

modern 发表于 2009-10-16 10:43:48

addresses()Hook method to capture the local and remote addresses for the service connection

If the service handler requires either the local or peer addresses on the new connection, it must implement the addresses() hook method to capture them when the connection is established. The ACE Proactor framework calls this method if the pass_address argument to the asynchronous connection factory was 1. This method is more significant on Windows because the connection addresses cannot be obtained any other way when asynchronous connection establishment is used.

winston 发表于 2009-10-16 14:40:58

系统调用这个,你在里面存储好就成。
页: [1]
查看完整版本: proactor的address方法