欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > C++使用Poco库指定网卡去接收组播数据

C++使用Poco库指定网卡去接收组播数据

2025/11/22 11:26:34 来源:https://blog.csdn.net/Love_XiaoQinEr/article/details/139806595  浏览:    关键词:C++使用Poco库指定网卡去接收组播数据
  • 0x00

    1、以下代码封装了一个UDP接收组播数据的类,第一个构造函数 Poco::Net::DatagramSocket 类可以实现UDP单播数据的接收、发送以及往组播发送数据,第二个构造函数使用Poco::Net::MulticastSocket类去接收组播数据,UdpUnit类中成员函数Recv仅仅实现了组播数据的接收,可以改成使用 Poco::Net::DatagramSocket 去接收单播数据。

  • 0x01

    2、Poco::Net::MulticastSocket在使用 joinGroup 函数加入组播时,可以只填写第一个参数"组播地址",Poco中Net库可以为你选择默认网卡加入组播,如果需要指定网卡加入组播时则使用 Poco::Net::NetworkInterface 类去选择需要的网卡。

  • 0x02

#ifndef UDP_UNIT_H
#define UDP_UNIT_H#include <string>
#include <Poco/Net/Socket.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/DatagramSocket.h>
#include <Poco/Net/MulticastSocket.h>
#include <Poco/Net/NetException.h>
#include <Poco/Net/NetworkInterface.h>#define RECV_BUFFER_SIZE 1024 * 1024 * 1class UdpUnit
{
public:UdpUnit(std::string const local_addr, int const local_port);UdpUnit(std::string const local_addr, std::string const multicast_addr, int const multicast_port);int Recv(std::string &recvMsg);int Recv(char *buffer, int bufferSize);int Send(const char *buffer, int bufferSize, const std::string remote_addr, const int remote_port);private:Poco::Net::SocketAddress m_localAddress;Poco::Net::DatagramSocket m_datagramSocket;Poco::Net::MulticastSocket m_multicastSocket;
};#endif

#include "udpUnit.h"UdpUnit::UdpUnit(std::string const local_addr, int const local_port) : m_localAddress(local_addr, local_port)
{// 单播数据try{m_datagramSocket.bind(m_localAddress, true);}catch (const Poco::Exception &ex){printf("Error: %s\n", ex.displayText().c_str());}
}UdpUnit::UdpUnit(std::string const local_addr, std::string const multicast_addr, int const multicast_port)
{// 组播数据try{m_multicastSocket.bind(Poco::Net::SocketAddress(multicast_port), true);// 指定网卡加入组播Poco::Net::IPAddress localAddr(local_addr);Poco::Net::NetworkInterface iInterface;iInterface.addAddress(localAddr);Poco::Net::IPAddress multicastAddr(multicast_addr);m_multicastSocket.joinGroup(multicastAddr, iInterface);}catch (const Poco::Exception &ex){printf("Error: %s\n", ex.displayText().c_str());}
}int UdpUnit::Recv(std::string &recvMsg)
{int bytesReceived = -1;try{char recvBuffer[RECV_BUFFER_SIZE];Poco::Net::SocketAddress otherAddr;bytesReceived = m_multicastSocket.receiveFrom(recvBuffer, RECV_BUFFER_SIZE, otherAddr);printf("Received from <%s:%d>\n", otherAddr.host().toString().c_str(), otherAddr.port());recvMsg.assign(recvBuffer, bytesReceived);}catch (const Poco::Exception &ex){printf("Exception: %s\n", ex.displayText().c_str());}return bytesReceived;
}int UdpUnit::Recv(char *buffer, int bufferSize)
{int bytesReceived = -1;try{Poco::Net::SocketAddress otherAddr;bytesReceived = m_multicastSocket.receiveFrom(buffer, bufferSize, otherAddr);printf("Received from <%s:%d>\n", otherAddr.host().toString().c_str(), otherAddr.port());}catch (const Poco::Exception &ex){printf("Exception: %s\n", ex.displayText().c_str());}return bytesReceived;
}int UdpUnit::Send(const char *buffer, int bufferSize, const std::string remote_addr, const int remote_port)
{Poco::Net::SocketAddress m_remoteAddress(remote_addr, remote_port);int bytesSend = m_datagramSocket.sendTo(buffer, bufferSize, m_remoteAddress);return bytesSend;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词