单次发送单次接收
下面的程序使用Proactor模式用UDP通信:
(1)发送端发送一个复合消息,并打印发送的内容
(2)接收端接收一个复合消息并打印接收到的内容
由于UDP是无连接的,所以这里没有Connector和Acceptor
本例是对ACE自带的example的稍微修改了一下(打印发送和接收的内容,这样更加直观)
发送端:client_main.cpp
#include
#include
#include
#include
#include
using namespace std; #include "ace/Reactor.h" #include "ace/Message_Queue.h" #include "ace/Asynch_IO.h" #include "ace/OS.h" #include "ace/Proactor.h" #include "ace/Asynch_Connector.h" #include
//============================================================================= /** * @file test_udp_proactor.cpp * * $Id: test_udp_proactor.cpp 93639 2011-03-24 13:32:13Z johnnyw $ * * This program illustrates how the
can be used to * implement an application that does asynchronous operations using * datagrams. * * * @author Irfan Pyarali
and Roger Tragin
*/ //============================================================================= #include "ace/OS_NS_string.h" #include "ace/OS_main.h" #include "ace/Proactor.h" #include "ace/Asynch_IO.h" #include "ace/INET_Addr.h" #include "ace/SOCK_Dgram.h" #include "ace/Message_Block.h" #include "ace/Get_Opt.h" #include "ace/Log_Msg.h" // Keep track of when we're done. static int done = 0; /** * @class Sender * * @brief The class will be created by
. */ class Sender : public ACE_Handler { public: Sender (void); ~Sender (void); //FUZZ: disable check_for_lack_ACE_OS ///FUZZ: enable check_for_lack_ACE_OS int open (const ACE_TCHAR *host, u_short port); protected: // These methods are called by the freamwork /// This is called when asynchronous writes from the dgram socket /// complete virtual void handle_write_dgram (const ACE_Asynch_Write_Dgram::Result &result); private: /// Network I/O handle ACE_SOCK_Dgram sock_dgram_; /// wd (write dgram): for writing to the socket ACE_Asynch_Write_Dgram wd_; const char* completion_key_; const char* act_; }; Sender::Sender (void) : completion_key_ ("Sender completion key"), act_ ("Sender ACT") { } Sender::~Sender (void) { this->sock_dgram_.close (); } int Sender::open (const ACE_TCHAR *host, u_short port) { // Initialize stuff if (this->sock_dgram_.open (ACE_INET_Addr::sap_any) == -1) ACE_ERROR_RETURN ((LM_ERROR, "[%D][line:%l]%p\n", "ACE_SOCK_Dgram::open"), -1); // Initialize the asynchronous read. if (this->wd_.open (*this, this->sock_dgram_.get_handle (), this->completion_key_, ACE_Proactor::instance ()) == -1) ACE_ERROR_RETURN ((LM_ERROR, "[%D][line:%l]%p\n", "ACE_Asynch_Write_Dgram::open"), -1); // We are using scatter/gather to send the message header and // message body using 2 buffers // create a message block for the message header ACE_Message_Block* msg = 0; ACE_NEW_RETURN (msg, ACE_Message_Block (100), -1); const char raw_msg [] = "To be or not to be."; // Copy buf into the Message_Block and update the wr_ptr (). msg->copy (raw_msg, ACE_OS::strlen (raw_msg) + 1); // create a message block for the message body ACE_Message_Block* body = 0; ACE_NEW_RETURN (body, ACE_Message_Block (100), -1); ACE_OS::memset (body->wr_ptr (), 'X', 100); body->wr_ptr (100); // always remember to update the wr_ptr () // set body as the cont of msg. This associates the 2 message blocks so // that a send will send the first block (which is the header) up to // length (), and use the cont () to get the next block to send. You can // chain up to IOV_MAX message block using this method. msg->cont (body); // do the asynch send size_t number_of_bytes_sent = 0; A