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 * 1

class 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;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓琴儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值