ace udp 组播

使用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;
}

转载于:https://my.oschina.net/u/262868/blog/95238

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值