【问题标题】:Differences between boost::asio and socket.h for multicast用于多播的 boost::asio 和 socket.h 之间的区别
【发布时间】:2023-03-07 17:06:02
【问题描述】:

我正在学习使用 socket.h 和 boost::asio 进行多播编程。我正在查看此链接here,他们使用 socket.h 提供以下代码来实现多播服务器。

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
struct in_addr localInterface;
struct sockaddr_in groupSock;
int sd;
char databuf[1024] = "Multicast test message lol!";
int datalen = sizeof(databuf);

int main (int argc, char *argv[ ])
{
/* Create a datagram socket on which to send. */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0)
{
  perror("Opening datagram socket error");
  exit(1);
}
else
  printf("Opening the datagram socket...OK.\n");

/* Initialize the group sockaddr structure with a */
/* group address of 225.1.1.1 and port 5555. */
memset((char *) &groupSock, 0, sizeof(groupSock));
groupSock.sin_family = AF_INET;
groupSock.sin_addr.s_addr = inet_addr("226.1.1.1");
groupSock.sin_port = htons(4321);

/* Disable loopback so you do not receive your own datagrams.
{
char loopch = 0;
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch, sizeof(loopch)) < 0)
{
perror("Setting IP_MULTICAST_LOOP error");
close(sd);
exit(1);
}
else
printf("Disabling the loopback...OK.\n");
}
*/

/* Set local interface for outbound multicast datagrams. */
/* The IP address specified must be associated with a local, */
/* multicast capable interface. */
localInterface.s_addr = inet_addr("203.106.93.94");
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0)
{
  perror("Setting local interface error");
  exit(1);
}
else
  printf("Setting the local interface...OK\n");
/* Send a message to the multicast group specified by the*/
/* groupSock sockaddr structure. */
/*int datalen = 1024;*/
if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0)
{perror("Sending datagram message error");}
else
  printf("Sending datagram message...OK\n");

/* Try the re-read from the socket if the loopback is not disable
if(read(sd, databuf, datalen) < 0)
{
perror("Reading datagram message error\n");
close(sd);
exit(1);
}
else
{
printf("Reading datagram message from client...OK\n");
printf("The message is: %s\n", databuf);
}
*/
return 0;
}

我还在查看如何使用 boost::asio here 实现多播服务器的示例,他们提供了以下代码。

//
// sender.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <sstream>
#include <string>
#include <boost/asio.hpp>
#include "boost/bind.hpp"
#include "boost/date_time/posix_time/posix_time_types.hpp"

const short multicast_port = 30001;
const int max_message_count = 10;

class sender
{
public:
  sender(boost::asio::io_service& io_service,
      const boost::asio::ip::address& multicast_address)
    : endpoint_(multicast_address, multicast_port),
      socket_(io_service, endpoint_.protocol()),
      timer_(io_service),
      message_count_(0)
  {
    std::ostringstream os;
    os << "Message " << message_count_++;
    message_ = os.str();

    socket_.async_send_to(
        boost::asio::buffer(message_), endpoint_,
        boost::bind(&sender::handle_send_to, this,
          boost::asio::placeholders::error));
  }

  void handle_send_to(const boost::system::error_code& error)
  {
    if (!error && message_count_ < max_message_count)
    {
      timer_.expires_from_now(boost::posix_time::seconds(1));
      timer_.async_wait(
          boost::bind(&sender::handle_timeout, this,
            boost::asio::placeholders::error));
    }
  }

  void handle_timeout(const boost::system::error_code& error)
  {
    if (!error)
    {
      std::ostringstream os;
      os << "Message " << message_count_++;
      message_ = os.str();

      socket_.async_send_to(
          boost::asio::buffer(message_), endpoint_,
          boost::bind(&sender::handle_send_to, this,
            boost::asio::placeholders::error));
    }
  }

private:
  boost::asio::ip::udp::endpoint endpoint_;
  boost::asio::ip::udp::socket socket_;
  boost::asio::deadline_timer timer_;
  int message_count_;
  std::string message_;
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: sender <multicast_address>\n";
      std::cerr << "  For IPv4, try:\n";
      std::cerr << "    sender 239.255.0.1\n";
      std::cerr << "  For IPv6, try:\n";
      std::cerr << "    sender ff31::8000:1234\n";
      return 1;
    }

    boost::asio::io_service io_service;
    sender s(io_service, boost::asio::ip::address::from_string(argv[1]));
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

我注意到使用 socket.h 的示例定义了本地接口和多播地址。然而,使用 boost::asio 的例子只定义了一个多播地址。为简洁起见,我不会包含代码,但我注意到使用 socket.h 和 boost::asio 实现多播接收器的代码定义了本地接口和多播地址。但是为什么我不需要使用 boost::asio 定义本地接口地址来实现多播服务器呢?另外,如果我想每隔几毫秒发送和接收多播消息,boost::asio 或 socket.h 会更快吗?

【问题讨论】:

  • 第二个答案只能通过基准来回答,这很可能是特定于应用程序的。

标签: c++ boost-asio multicast


【解决方案1】:

当使用多播时,只需要在数据报应该传出特定接口时设置IP_MULTICAST_IF 选项。 Boost.Asio 通过ip::multicast::outbound_interface 提供了这个选项。当不使用该选项时,组播传输从默认接口发送,内核可能会通过其他接口进行路由和转发。例如,考虑服务器有两个 NIC 卡,将其连接到 LAN 和 WAN 的情况。如果 WAN 是默认接口,并且多播数据报要发送到 LAN,那么对于给定的套接字,可以使用 socket 选项将出站接口指定为 LAN。


发送方通常很少关心套接字绑定到的确切端点(地址和端口)。在这两个发送者示例中,发送者创建一个套接字,并按照内核绑定到一个端点。在第一个示例中,从本地套接字发送的多播消息将离开已分配 203.106.93.94 地址的接口。

另一方面,接收方通常关心绑定到特定端口。接收者会将本地套接字绑定到任何适当的地址或遵循内核,并绑定到与多播端点端口匹配的端口。一旦绑定,接收者将让套接字加入多播组,此时套接字可以开始接收多播数据报。请注意,对于给定的系统,如果多个应用程序有兴趣接收多播数据报,则应使用reuse_address 套接字选项。

使用 Boost.Asio 示例作为参考,如果使用./sender 239.255.0.1 启动发送方,使用./receiver 0.0.0.0 239.255.0.1 启动多个接收方,则会发生以下套接字和绑定:

                                                                  .----------.
                                                                 .----------.|
.--------. address: any                            address: any .----------.||
|        | port: any      /                    \   port: 30001 |          |||
| sender |-( ----------->| address: 239.255.0.1 |----------> )-| receiver ||'
|        |                \   port: 30001      /               |          |'
'--------'                                                     '----------'
  • 发件人绑定到任何地址和端口。例如,假设内核将其绑定到端口24000
  • 接收者绑定到任意地址和端口30001并加入239.255.0.1多播组。
  • 发件人将消息写入239.255.0.1:30001
  • 接收方接收发送到239.255.0.1:30001 的消息。接收者的receive_from() 操作的sender_endpoint 参数将填充发送者的端点地址和端口24000

就性能而言,分析应用程序将提供明确的答案。问题中提供的示例非常不同(同步与异步),因此直接比较两者以确定哪个更快可能不合适。一般来说,Boost.Asio 会因为它的抽象而提供一些开销。但是,我还没有开发过 Boost.Asio 的开销是问题的应用程序,它的抽象为我节省了无数的开发和维护工时。

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2018-08-23
    • 2016-05-12
    • 1970-01-01
    相关资源
    最近更新 更多