|
服务器端在Proactive模式启动异步接受连接
客户端用多线程模拟多客户端并发连接服务器,但是服务器在接受各个线程的连接之后都会收到一个长度为0的消息,而最后一个线程却不会。
当启动多个进程,每个进程有一线程去连接时,服务器 在接受连接后却不会收到长度为0的消息
请问大侠这是为什么?
代码如下:
服务器端
// TestFile.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ace/OS_main.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/ACE.h"
#include "ace/Service_Object.h"
#include "ace/Asynch_IO.h"
#include "ace/Proactor.h"
#include "ace/message_block.h"
#include "ace/Asynch_Acceptor.h"
#include "ace/INET_Addr.h"
class Echo_Service : public ACE_Service_Handler
{
public:
Echo_Service()
{
ACE_DEBUG((LM_DEBUG,"(%t) 新连接到来\n"));
}
~Echo_Service ()
{
ACE_DEBUG((LM_DEBUG,"(%t) 客户端连接被关闭\n"));
if (this->handle () != ACE_INVALID_HANDLE)
ACE_OS::closesocket (this->handle ());
}
virtual void open (ACE_HANDLE h, ACE_Message_Block&)
{
ACE_DEBUG((LM_DEBUG,"(%t) 连接成功\n"));
this->handle (h);
if (this->reader_.open (*this) != 0 || this->writer_.open (*this) != 0 )
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),ACE_TEXT ("Echo_Service open")));
delete this;
return;
}
ACE_Message_Block *mb;
ACE_NEW_NORETURN (mb, ACE_Message_Block (1024));
if (this->reader_.read (*mb, mb->space ()) != 0)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),ACE_TEXT ("Echo_Service begin read")));
mb->release ();
delete this;
return;
}
}
virtual void handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{
ACE_Message_Block &mb = result.message_block ();
int cb = result.bytes_transferred () ;
if (!result.success () || cb == 0)
{
if(!cb && result.success())
{
ACE_DEBUG((LM_DEBUG,"(%t) 收到消息长度为零\n"));
}
else
{
ACE_DEBUG((LM_DEBUG,"(%t) 读取数据失败\n"));
}
mb.release ();
delete this;
}
else
{
if (this->writer_.write (mb, mb.length ()) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("starting write")));
mb.release ();
}
else
{
ACE_Message_Block *new_mb;
ACE_NEW_NORETURN (new_mb, ACE_Message_Block (1024));
this->reader_.read (*new_mb, new_mb->space ());
}
}
return;
}
virtual void handle_write_stream
(const ACE_Asynch_Write_Stream::Result &result)
{
ACE_Message_Block &mb = result.message_block ();
mb.release ();
return;
}
private:
ACE_Asynch_Read_Stream reader_;
ACE_Asynch_Write_Stream writer_;
};
typedef ACE_Asynch_Acceptor<Echo_Service> MyAcceptor;
int main(int argc, char *argv[])
{
ACE_INET_Addr addr(1500);
MyAcceptor server;
if(server.open(addr)==-1)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%P|%t) %p\n"),
ACE_TEXT ("bind failed")));
return 1;
}
while(1){
ACE_Proactor::instance ()->proactor_run_event_loop ();
}
return 0;
}
客户端:
在客户端启动多个线程,每个线程中都会调:
sock_conn.connect (sock_stream, addr)
去连接服务器,在服务器端已经显示在连接成功后回调Open方法。但去做异步读回调中收到了零尺度的消息。导致连接被关闭。
我做这个的目的是测试服务器接收并发连接的能力。 |
|