使用ace6.1.0验证。
Recv:
int CMulticastReceiver::start(const char* ip, const unsigned short usPort){ ACE_INET_Addr tempAddr(usPort, ip); m_cast_addr_.set(tempAddr); // Subscribe to multicast address. if (m_cast_dgram_.join(m_cast_addr_) == -1) { std::cout << "Error in subscribing to Multicast address " << std::endl; return -1; } //get ready to receive data from the sender. ACE_INET_Addr remote_addr_; char buff[100] = {0}; if(m_cast_dgram_.recv (buff, 100, remote_addr_) == -1) { std::cout << "error happen when recv" << std::endl; return -1; } std::cout << " Received multicast from :" << remote_addr_.get_host_name() << " " << remote_addr_.get_port_number() << " " << buff << std::endl; return 0;}int CMulticastReceiver::stop(){ if(m_cast_dgram_.leave(m_cast_addr_) == -1) { std::cout << "some error happen when leave.. " << std::endl; return -1; } std::cout << "multicast leave success.." << std::endl; return 0;}
Send:
#define DEFAULT_MULTICAST_ADDR "224.2.2.2"int send_multicast(const ACE_INET_Addr &mcast_addr){ const char *message = "this is the message!\n"; ACE_SOCK_Dgram_Mcast udp; if (-1 == udp.join (mcast_addr)) { ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("join")), -1); std::cout << "join error" << std::endl; return -1; } ssize_t sent = udp.send (message, ACE_OS_String::strlen (message) + 1); udp.close(); if (sent == -1) { ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("send")), -1); std::cout << "send error" << std::endl; return -1; } std::cout << "send success!.." << std::endl; return 0;}