我正在学习使用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会更快吗?
答案 0 :(得分:1)
使用多播时,只需要在数据报输出特定接口时设置IP_MULTICAST_IF
选项。 Boost.Asio使用ip::multicast::outbound_interface
提供此选项。如果不使用此选项,则从默认接口发送多播传输,内核可以通过其他接口执行路由和转发。例如,考虑服务器有两个NIC卡,将其连接到LAN和WAN的情况。如果WAN是默认接口,并且组播数据报要发送到LAN,那么对于给定的套接字,可以使用套接字选项将出站接口指定为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的开销是问题,它的抽象为我节省了无数的开发和维护工时。