一个 ACE TCP Server/Client 例子

本文介绍了一个使用ACE库实现的基本网络通信程序,包括服务器端和客户端的代码示例。服务器监听指定端口并响应客户端连接请求,接收客户端发送的数据,并回复消息。客户端则连接到服务器,发送一条消息并接收服务器的回应。
摘要由CSDN通过智能技术生成
 

// Server
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/OS_NS_stdio.h"
#include "ace/Log_Msg.h"

#define SIZE_DATA 18
#define SIZE_BUF 1024
#define NO_ITERATIONS 1

class Server
{
public:
 Server (int port): server_addr_(port),peer_acceptor_(server_addr_)
 {
  data_buf_= new char[SIZE_BUF];
 }
 
 int handle_connection();
 int accept_connections ();
 
private:
 char *data_buf_;
 
 ACE_INET_Addr server_addr_;
 ACE_INET_Addr client_addr_;
 ACE_SOCK_Acceptor peer_acceptor_;
 ACE_SOCK_Stream new_stream_;
};

int
Server::handle_connection()
{
 for(int i=0;i {
  int byte_count=0;
  
  if( (byte_count=new_stream_.recv_n (data_buf_, SIZE_DATA))==-1)
  {
   ACE_ERROR ((LM_ERROR, "%pn", "Error in recv"));
  }
  else
  {
   data_buf_[byte_count]=0;
   ACE_DEBUG((LM_DEBUG,"Server received: %sn",data_buf_));
  }
 }
 
 int n_bytes = 0;
 if( ( n_bytes=new_stream_.send_n( "Hi, World from Server", 21))==-1)
 {
  ACE_ERROR ((LM_ERROR, "%pn", "Error in send"));
 }
 else
 {
  ACE_DEBUG((LM_DEBUG,"Server send okn"));
 }
 
 if (new_stream_.close () == -1)
 {
  ACE_ERROR ((LM_ERROR, "%pn", "close"));
 }
 
 return 0;
}

int
Server::accept_connections ()
{
 if (peer_acceptor_.get_local_addr (server_addr_) == -1)
  ACE_ERROR_RETURN ((LM_ERROR,"%pn","Error in get_local_addr"),1);
 
 ACE_DEBUG ((LM_DEBUG,"Starting server at port %dn",
  server_addr_.get_port_number ()));
 
 while(1)
 {
  ACE_Time_Value timeout (20);
  
  if (peer_acceptor_.accept (new_stream_, &client_addr_, &timeout)== -1)
  {
   ACE_ERROR ((LM_ERROR, "n%pn", "accept"));
   continue;
  }
  else
  {
   ACE_DEBUG((LM_DEBUG,
    "nConnection established with remote %s:%dn",
    client_addr_.get_host_name(),client_addr_.get_port_number()));
   
   handle_connection();
  }
 }
}

int main (int argc, char *argv[])
{
 static const u_short port=1009;
 
 Server server(port);
 
 server.accept_connections();
 
 return 0;
}


===============================================================================
// Client
#include "ace/SOCK_Connector.h"
#include "ace/INET_Addr.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"

#define SIZE_BUF 128
#define NO_ITERATIONS 1

class Client
{
public:
 Client(char *hostname, int port):remote_addr_(port,hostname)
 {
  data_buf_="Hello from Client";
 }
 
 int connect_to_server();
 int send_to_server();
 int close();
 
private:
 ACE_SOCK_Stream client_stream_;
 ACE_INET_Addr remote_addr_;
 ACE_SOCK_Connector connector_;
 
 char *data_buf_;
};

int
Client::connect_to_server()
{
 ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting connect to %s:%dn",
  remote_addr_.get_host_name(),remote_addr_.get_port_number()));
 
 if (connector_.connect (client_stream_, remote_addr_) == -1)
 {
  ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %pn","connection failed"),-1);
 }
 else
 {
  ACE_DEBUG ((LM_DEBUG,"(%P|%t) connected to %sn",
   remote_addr_.get_host_name ()));
 }
 
 return 0;
}

int
Client::send_to_server()
{
 for(int i=0; i {
  int n = 0;
  n = client_stream_.send_n (data_buf_, ACE_OS::strlen(data_buf_)+1);
  if ( n == -1)
  {
   ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %pn","send_n"),0);
   break;
  }
  
  // recv
  char recv_buff[ 32 ] = { 0 };
  n=client_stream_.recv_n (recv_buff, 32 );
  if( n ==-1)
  {
   ACE_ERROR ((LM_ERROR, "%pn", "Error in recv"));
  }
  else
  {
   // data_buf_[byte_count]=0;
   ACE_DEBUG((LM_DEBUG,"Server received: %sn",recv_buff));
  }
  
 }
 
 return 0;
}

int
Client::close()
{
 if (client_stream_.close () == -1)
  ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %pn","close"),-1);
 else
  return 0;
}

static const char* SERVER_HOST = "127.0.0.1";
static u_short SERVER_PORT = 1009;

int main (int argc, char *argv[])
{
 Client client( "127.0.0.1",SERVER_PORT);
 
 client.connect_to_server();
 
 client.send_to_server();
 
 client.close();
 
 // ACE_OS::sleep( 1 );
 
 return 0;
}

下面是一个使用ACE框架的TCP SERVER的Demo代码: ```c++ #include "ace/INET_Addr.h" #include "ace/OS.h" #include "ace/SOCK_Acceptor.h" #include "ace/SOCK_Stream.h" class MyTCPHandler : public ACE_Event_Handler { public: MyTCPHandler() {} virtual ~MyTCPHandler() {} virtual ACE_HANDLE get_handle() const { return peer_.get_handle(); } virtual int handle_input(ACE_HANDLE fd) { char buf[1024]; ssize_t n = peer_.recv(buf, sizeof(buf)); if (n <= 0) { ACE_DEBUG((LM_DEBUG, "Connection closed.\n")); return -1; } ACE_DEBUG((LM_DEBUG, "Received %d bytes: %s\n", n, buf)); return 0; } virtual int handle_close(ACE_HANDLE fd, ACE_Reactor_Mask mask) { ACE_DEBUG((LM_DEBUG, "Connection closed.\n")); delete this; return 0; } void set_peer(const ACE_SOCK_Stream& peer) { peer_ = peer; } private: ACE_SOCK_Stream peer_; }; int main(int argc, char* argv[]) { ACE_INET_Addr listen_addr(8888); ACE_SOCK_Acceptor acceptor; if (acceptor.open(listen_addr) == -1) { ACE_ERROR((LM_ERROR, "%p\n", "open")); return 1; } ACE_Reactor reactor; ACE_Time_Value timeout(0, 100 * 1000); while (true) { ACE_SOCK_Stream peer; ACE_INET_Addr peer_addr; if (acceptor.accept(peer, &peer_addr) == -1) { ACE_ERROR((LM_ERROR, "%p\n", "accept")); continue; } ACE_DEBUG((LM_DEBUG, "Connected: %s\n", peer_addr.get_host_name())); MyTCPHandler* handler = new MyTCPHandler(); handler->set_peer(peer); reactor.register_handler(handler, ACE_Event_Handler::READ_MASK); } return 0; } ``` 这个Demo创建了一个TCP Server,监听8888端口,当有客户端连接时,将创建一个MyTCPHandler对象来处理连接。MyTCPHandler继承自ACE_Event_Handler,重载了handle_input和handle_close方法。当有数据可读时,handle_input方法将被调用,读取数据并输出到控制台。当连接关闭时,handle_close方法将被调用,关闭连接并删除这个MyTCPHandler对象。 在主函数中,使用ACE_SOCK_Acceptor类监听指定地址和端口,当有客户端连接时,创建一个MyTCPHandler对象并注册到ACE_Reactor中。循环等待客户端连接。 当客户端连接时,程序将创建一个ACE_SOCK_Stream对象,该对象将作为MyTCPHandler的peer_成员,保存客户端连接信息。然后创建一个MyTCPHandler对象,并将其注册到ACE_Reactor中,以便处理连接。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值