An ACE TCP server/client example

Source: Internet
Author: User

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;
 &NBSP
  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") ==-1)
 {
  ace_error ((lm_ Error, "%PN", "Error in Send"));
 }
 else
 {
  ace_debug ((lm_debug, "Server send Okn");
&NBSP}
 
 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;
 &NBSP
  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 ();
 &NBSP}
 }

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[] = {0};
  n=client_stream_.recv_n (Recv_buff, 32);
  if (n ==-1)
  {
   ace_error ((Lm_error, "%PN", "ERROR in Recv"));
 &NBSP}
  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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.