c++ - 如何让两个多播套接字监听具有相同端口的两个多播 channel

标签 c++ c sockets multicast multicastsocket

我大致有以下多播套接字代码。效果很好。现在我需要在同一台机器上加入两个多播 channel ,例如 224.10.13.18 - 55001 224.10.13.34 - 55001

并且根据它来自哪个 IP 地址,我需要以不同的方式处理该消息。

问题是如何为端口值相同的多播 channel 创建两个套接字,以便每个套接字仅返回对发送到该 channel 的数据的读取。

  /* create socket to join multicast group on */
  socket_file_descriptor_ = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  if ( socket_file_descriptor_ < 0 ) 
    { // fprintf ( stderr, "cannot open socket \n");
      exit(1);
    }

  /* set reuse port to on to allow multiple binds per host */ 
  {
    int flag_on = 1;
    if ( ( setsockopt ( socket_file_descriptor_, SOL_SOCKET, SO_REUSEADDR, &flag_on,
                        sizeof(flag_on) ) ) < 0 ) 
      { // fprintf ( stderr, "MulticastReceiverSocket setsockopt() SOL_SOCKET SO_REUSEADDR failed\n");
        exit(1);
      }
  } 

  struct ip_mreq mc_req;
  inet_pton ( AF_INET, listen_ip_.c_str(), &(mc_req.imr_multiaddr.s_addr) ); 

  mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

  /* send an ADD MEMBERSHIP message via setsockopt */
  if ( ( setsockopt ( socket_file_descriptor_, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
                      (void*) &mc_req, sizeof(mc_req))) < 0) 
  { // std::cerr << "setsockopt() failed in IP_ADD_MEMBERSHIP " << listen_ip_ << ": "<< listen_port_ << std::endl;
      exit(1);
  } 


  /* construct a multicast address structure */
  struct sockaddr_in mcast_Addr;
  bzero ( &mcast_Addr, sizeof(mcast_Addr) );
  mcast_Addr.sin_family = AF_INET;
  mcast_Addr.sin_addr.s_addr = htonl(INADDR_ANY);
  mcast_Addr.sin_port = htons ( listen_port_ );
  /* bind to specified port onany interface */
  if ( bind ( socket_file_descriptor_, (struct sockaddr *) &mcast_Addr, sizeof ( struct sockaddr_in ) ) < 0 ) 
  { // fprintf ( stderr, "%s cannot bind %s:%d \n", "MulticastReceiverSocket", listen_ip_.c_str(), listen_port_ ) ;
     exit(1);
  } 

最佳答案

为此,您只需要一个套接字。如果您在调用 setsockopt 时设置了 IP_PKTINFO 选项,则可以使用 recvmsg 获取 struct in_pktinfo,其中将包含目标 IP 地址。然后您可以根据该信息选择如何处理数据包。

借用https://stackoverflow.com/a/5309155/1687119 (为简洁起见,删除了错误检查):

// sock is bound AF_INET socket, usually SOCK_DGRAM
// include struct in_pktinfo in the message "ancilliary" control data
setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt));
// the control data is dumped here
char cmbuf[0x100];
// the remote/source sockaddr is put here
struct sockaddr_in peeraddr;
// if you want access to the data you need to init the msg_iovec fields
struct msghdr mh = {
    .msg_name = &peeraddr,
    .msg_namelen = sizeof(peeraddr),
    .msg_control = cmbuf,
    .msg_controllen = sizeof(cmbuf),
};
recvmsg(sock, &mh, 0);
for ( // iterate through all the control headers
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh);
    cmsg != NULL;
    cmsg = CMSG_NXTHDR(&mh, cmsg))
{
    // ignore the control headers that don't match what we want
    if (cmsg->cmsg_level != IPPROTO_IP ||
        cmsg->cmsg_type != IP_PKTINFO)
    {
        continue;
    }
    struct in_pktinfo *pi = CMSG_DATA(cmsg);
    // at this point, peeraddr is the source sockaddr
    // pi->ipi_spec_dst is the destination in_addr
    // pi->ipi_addr is the receiving interface in_addr
}

关于c++ - 如何让两个多播套接字监听具有相同端口的两个多播 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20691573/

相关文章:

C: listen() API 返回 -1

c++ - Qt Regex 帮助(数组键)

c++ - boost c++库源中nat的含义

apache-flex - 在 Flex 中通过二进制套接字发送字符零 "\0"

c - 如何用C语言制作乘法表

c - 2个while循环内的变量不会重置

python - 在应用程序之间共享套接字有什么好处?

c++ - OpenMP 卸载到 Nvidia 错误减少

c++ - 有效地将大量类添加到 C++ 中的映射数组

c - lpm rd,Z 总是在 gcc-avr 中的内联汇编上转换为 lpm rd,Z+