Example 3
Author: Ambreen Ilyas

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// 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 3

//Server
#include "ace/OS.h"
#include "ace/SOCK_Dgram.h"
#include "ace/INET_Addr.h"

#define DATA_BUFFER_SIZE 1024
#define SIZE_DATA 18

class Server{
public:
Server(int local_port)
:local_addr_(local_port),local_(local_addr_){
data_buf = new char[DATA_BUFFER_SIZE];
}
//Expect data to arrive from the remote machine. Accept it and display it.
// After recieveing data immediately send some data back to the remote.
int accept_data(){
while(local_.recv(data_buf,SIZE_DATA,remote_addr_)!=-1){
ACE_DEBUG((LM_DEBUG, "Data received from remote %s was %s \n"
,remote_addr_.get_host_name(), data_buf));
ACE_OS::sleep(1);
if(send_data()==-1) break;
}
return -1;
}

//Method used to send data to the remote using the datagram component local_
int send_data(){
ACE_DEBUG((LM_DEBUG,"Preparing to send reply to client %s:%d\n",
remote_addr_.get_host_name(),remote_addr_.get_port_number()));
ACE_OS::sprintf(data_buf,"Server says hello to you too");
if(
local_.send(data_buf, ACE_OS::strlen(data_buf),remote_addr_)==-1)
return -1;
else
return 0;
}

private:
char *data_buf;
ACE_INET_Addr remote_addr_;
ACE_INET_Addr local_addr_;
ACE_SOCK_Dgram local_;
};

int main(int argc, char *argv[]){
Server server(ACE_OS::atoi(argv[1]));
server.accept_data();
}

Next Example