ACE源碼樣本 – IPC SAP

來源:互聯網
上載者:User

以下樣本是在《 ACE programmers guide》中已經發布過的. 這些代碼都是出自Hughes Network Systems. 如有疑問可以發郵件給 Umar Syyid ,或者與我交流hxhforwork@hotmail.com

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// This example is from the ACE Programmers Guide.
////  Chapter: "IPC SAP" (Interprocess Communication Mechanisms in ACE).
//// For details please see the guide at
//// http://www.cs.wustl.edu/~schmidt/ACE.html
////  AUTHOR: Umar Syyid (usyyid@hns.com)
//// and Ambreen Ilyas (ambreen@bitsmart.com)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Example 1
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#define SIZE_DATA 18
#define SIZE_BUF 1024

class Server{

public:
Server (int port):
 server_addr_(port),peer_acceptor_(server_addr_){
 data_buf_= new char[SIZE_BUF];
 }

//Handle the connection once it has been established.
//Here the connection is handled by reading SIZE_DATA amount of data
//from the remote and then closing the connection
//stream down.
int handle_connection(){
  // Read data from client
 if(new_stream_.recv_n (data_buf_, SIZE_DATA, 0)==-1)
  ACE_ERROR ((LM_ERROR, "%p/n", "Error in recv"));
 

 ACE_DEBUG((LM_DEBUG,"Server recieved %s /n",data_buf_));
 
 // Close new endpoint
 if (new_stream_.close () == -1)
  ACE_ERROR ((LM_ERROR, "%p/n", "close"));
 return 0;
}
//Use the acceptor component peer_acceptor_ to accept the connection
//into the underlying stream new_stream_. After the connection has been
//established call the handle_connenction() method.
int accept_connections (){
 if (peer_acceptor_.get_local_addr (server_addr_) == -1)
  ACE_ERROR_RETURN ((LM_ERROR,"%p/n","Error in get_local_addr"),1);

 ACE_DEBUG ((LM_DEBUG,"Starting server at port %d/n",
  server_addr_.get_port_number ()));
 

 // Performs the iterative server activities.
 while(1){
  ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
  if (peer_acceptor_.accept
    (new_stream_, &client_addr_, &timeout)== -1){
   ACE_ERROR ((LM_ERROR, "%p/n", "accept"));
   continue;
   }
  else
   ACE_DEBUG((LM_DEBUG,
   "Connection established with remote %s:%d/n",
   client_addr_.get_host_name(),client_addr_.get_port_number()));
   //Handle the connection
   handle_connection();
  }
 }
 
 

private:
 char *data_buf_;
 ACE_INET_Addr server_addr_;
 ACE_INET_Addr client_addr_;
 ACE_SOCK_Acceptor peer_acceptor_;
 ACE_SOCK_Stream new_stream_;
 ACE_HANDLE newhandle;
};

int main (int argc, char *argv[]){
 if(argc<2){
  ACE_ERROR((LM_ERROR,"Usage egX"));
  ACE_OS::exit(1);
  }
 Server server(ACE_OS::atoi(argv[1]));
 server.accept_connections();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.