|
Server和Client的程序都是正确的,但是两者无法建立连接,求高手指点下,在linux下运行的。
服务端:
#pragma once
#ifndef __SERVER_H_
#define __SERVER_H_
#include "ace/Svc_Handler.h"
#include "ace/SOCK_Stream.h"
#include "ace/SOCK_Dgram.h"
#include "ace/Synch.h"
#include "ace/Thread_Manager.h"
#include "ace/Task.h"
#include "ace/Message_Block.h"
namespace ACE_Server
{
static const size_t SERVER_THREAD_POOL_SIZE = 5; //进行数据接收的线程池大小
static const size_t TASK_THREAD_POOL_SIZE = 5; //进行数据处理的线程池大小
static const size_t BUFFER_SIZE = 4096; //数据缓冲区大小
static const size_t SERVER_PORT_NUM = 10101; //服务器的通信端口号
class Server: public ACE_Task_Base
{
public:
virtual int svc(void);
};
class Task_Worker: public ACE_Task<ACE_MT_SYNCH>
{
public:
virtual int svc(void);
private:
void process_task(ACE_Message_Block *mb);
};
class Task_Manager: public ACE_Task<ACE_MT_SYNCH>
{
public:
virtual int svc(void);
};
class Request_Handler: public ACE_Event_Handler
{
public:
Request_Handler(const ACE_INET_Addr &local_addr);
virtual ACE_HANDLE get_handle (void) const;
virtual int handle_close (ACE_HANDLE handle,ACE_Reactor_Mask);
virtual int handle_timeout (const ACE_Time_Value & tv, const void *arg = 0);
protected:
virtual int handle_input(ACE_HANDLE fd = ACE_INVALID_HANDLE);
private:
static Task_Manager task_mgr;
ACE_SOCK_Dgram endpoint_;
};
}
#endif
#include "ace/Message_Block.h"
#include "Server.h"
namespace ACE_Server
{
int Task_Manager::svc(void)
{
Task_Worker task_tp;
task_tp.activate(THR_NEW_LWP | THR_JOINABLE, TASK_THREAD_POOL_SIZE);
while(1)
{
ACE_Message_Block *mb = NULL;
if(this->getq(mb) < 0)
{
task_tp.msg_queue()->deactivate();
task_tp.wait();
}
task_tp.putq(mb);
}
return 0;
}
}
#include "ace/OS.h"
#include "ace/Message_Block.h"
#include "Server.h"
namespace ACE_Server
{
int Task_Worker::svc(void)
{
while(1)
{
ACE_Message_Block *mb = NULL;
if(this->getq(mb) == -1)
{
continue;
}
process_task(mb);
}
return 0;
}
void Task_Worker::process_task(ACE_Message_Block *mb)
{
//进行数据处理,数据的起始地址为mb->rd_ptr(),长度为mb->length()
ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%t) Processing task: %s length %d\n"), mb->rd_ptr()+4, mb->length())); //字符串跳过了四个字节的长度数据
ACE_OS::sleep(3); //模拟数据处理过程
mb->release();
}
}
#include "ace/Reactor.h"
#include "Server.h"
namespace ACE_Server
{
int Server::svc(void)
{
int result = ACE_Reactor::instance()->run_reactor_event_loop();
if(result == -1)
return -1;
return 0;
}
}
#include "ace/OS.h"
#include "ace/Message_Block.h"
#include "ace/Thread_Manager.h"
#include "ace/Svc_Handler.h"
#include "ace/SOCK_Stream.h"
#include "ace/Synch.h"
#include "ace/Reactor.h"
#include "Server.h"
namespace ACE_Server
{
Task_Manager Request_Handler::task_mgr;
Request_Handler::Request_Handler(const ACE_INET_Addr &local_addr)
: endpoint_ (local_addr)
{
//this->reactor(ACE_Reactor::instance());
task_mgr.activate();
}
ACE_HANDLE Request_Handler::get_handle (void) const
{
return this->endpoint_.get_handle ();
}
int Request_Handler::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask)
{
ACE_UNUSED_ARG (handle);
ACE_DEBUG ((LM_DEBUG, "(%P|%t) handle_close\n"));
this->endpoint_.close ();
delete this;
return 0;
}
int Request_Handler::handle_timeout (const ACE_Time_Value &,
const void *)
{
ACE_DEBUG ((LM_DEBUG, "(%P|%t) timed out for endpoint\n"));
return 0;
}
int Request_Handler::handle_input(ACE_HANDLE fd)
{
char buf[BUFSIZ];
ACE_INET_Addr from_addr;
ACE_DEBUG ((LM_DEBUG, "(%P|%t) activity occurred on handle %d!\n", this->endpoint_.get_handle ()));
ssize_t n = this->endpoint_.recv (buf, sizeof buf, from_addr);
if (n == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "handle_input"));
else
{
ACE_DEBUG ((LM_DEBUG, "(%P|%t) buf of size %d = %*s\n", n, n, &buf[4]));//字符串跳过了四个字节的长度数据
ACE_Message_Block *mb = NULL;
ACE_NEW_RETURN(mb, ACE_Message_Block(n, ACE_Message_Block::MB_DATA, 0, buf), -1);
mb->wr_ptr(n);
task_mgr.putq(mb);
}
return 0;
}
}
#include "ace/SOCK_Acceptor.h"
#include "ace/Acceptor.h"
#include "ace/Thread_Manager.h"
#include "ace/TP_Reactor.h"
#include "ace/Reactor.h"
#include "ace/INET_Addr.h"
#include "ace/OS.h"
#include "Server.h"
using namespace ACE_Server;
int main(int argc, char *argv[])
{
//设置服务器地址
ACE_INET_Addr local_addr(SERVER_PORT_NUM, "localhost");
Request_Handler *endpoint;
ACE_NEW_RETURN (endpoint,Request_Handler(local_addr),-1);
// Read data from other side.
if (ACE_Reactor::instance ()->register_handler(endpoint,ACE_Event_Handler::READ_MASK) == -1)
ACE_ERROR_RETURN ((LM_ERROR,"ACE_Reactor::register_handler"),-1);
Server server_tp;
server_tp.activate(THR_NEW_LWP | THR_JOINABLE, SERVER_THREAD_POOL_SIZE);
ACE_Thread_Manager::instance()->wait();
return 0;
}
客户端:
#ifndef __CONSTANTS_H_
#define __CONSTANTS_H_
#include "ace/Time_Value.h"
namespace ACE_Client
{
static const size_t BUFFER_SIZE = 4096; //数据缓冲区大小
static const size_t SERVER_PORT_NUM = 10101; //服务器的通信端口号
static const ACE_Time_Value TIME_INTERVAL(0, 1000000); //两次数据发送之间的时间间隔(0 s + 1000000 us = 1 s)
}
#endif
#ifndef __CLIENT_H_
#define __CLIENT_H_
#include "ace/Task.h"
#include "ace/INET_Addr.h"
#include "ace/Synch.h"
namespace ACE_Client
{
class Client: public ACE_Task<ACE_NULL_SYNCH>
{
public:
Client(char *server);
virtual int svc(void);
char *put_task(char *msg_t, char *msg_s, size_t msg_len);
void send_tasks(void);
private:
ACE_INET_Addr addr;
};
}
#endif
#include "ace/OS.h"
#include "ace/SOCK_Dgram.h"
#include "ace/Message_Block.h"
#include "ace/Thread_Manager.h"
#include "ace/INET_Addr.h"
#include "Constants.h"
#include "Client.h"
namespace ACE_Client
{
Client::Client(char *server)
{
addr = ACE_INET_Addr(SERVER_PORT_NUM, server);
}
int Client::svc(void)
{
ACE_SOCK_Dgram dgram(ACE_INET_Addr((u_short)0));
while(1)
{
ACE_Message_Block *mb = NULL;
if(this->getq(mb) == -1)
{
break;
}
ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%t) Sending %s\n"), &(mb->rd_ptr()[4])));
dgram.send(mb->rd_ptr(), mb->length(), addr);
mb->release();
ACE_OS::sleep(TIME_INTERVAL);
}
return 0;
}
char *Client::put_task(char *msg_t, char *msg_s, size_t msg_len)
{
for(int i = 0; i < 4; i++)
{
msg_t[i] = (char)((msg_len >> (8 * i)) & 0xff);
}
ACE_OS::memcpy(&msg_t[4], msg_s, msg_len);
ACE_Message_Block *mb = NULL;
ACE_NEW_RETURN(mb, ACE_Message_Block(msg_len + 4, ACE_Message_Block::MB_DATA, 0, msg_t), 0);
mb->wr_ptr(msg_len + 4);
this->putq(mb);
return msg_t;
}
void Client::send_tasks(void)
{
this->activate();
ACE_Thread_Manager::instance()->wait();
}
}
#include "Client.h"
using namespace ACE_Client;
int main(int argc, char *argv[])
{
ACE::init();//初始化ACE库,在windows下一定要
Client client("localhost"); //服务器的IP地址或者服务器名称
for(int i = 0; i < 5; i++)
{
char *task1 = "Is it a good day?"; //第1个task的数据
size_t task1_len = 18; //第1个task的数据长度
char *task1_t; //无需修改
ACE_NEW_RETURN(task1_t, char[task1_len + 4], -1); //无需修改
client.put_task(task1_t, task1, task1_len); //无需修改
char *task2 = "Yeah, it really is."; //第2个task的数据
size_t task2_len = 20; //第2个task的数据长度
char *task2_t; //无需修改
ACE_NEW_RETURN(task2_t, char[task2_len + 4], -1); //无需修改
client.put_task(task2_t, task2, task2_len); //无需修改
client.send_tasks(); //将上面的task全部发到服务器
delete [] task1_t; //释放task1的内存
delete [] task2_t; //释放task2的内存
}
ACE::fini();
system("pause");
return 0;
}
客户端的东西不能发送到服务端。
|
|