在所有接口上侦听多播

时间:2011-05-25 13:52:49

标签: c++ sockets network-programming

我有以下界面:

ifconfig
bond0     Link encap:Ethernet  HWaddr 1C:C1:DE:1F:71:CA  
          inet addr:10.3.3.13  Bcast:10.3.3.255  Mask:255.255.255.0
          inet6 addr: fe80::1ec1:deff:fe1f:71ca/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:39972916 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21849588 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:60630723698 (56.4 GiB)  TX bytes:1570500163 (1.4 GiB)

eth0      Link encap:Ethernet  HWaddr 1C:C1:DE:1F:71:CA  
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:39959516 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21849585 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:60629214902 (56.4 GiB)  TX bytes:1570499971 (1.4 GiB)
          Interrupt:16 Memory:f2000000-f2012800 

eth1      Link encap:Ethernet  HWaddr 1C:C1:DE:1F:71:CA  
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:13400 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1508796 (1.4 MiB)  TX bytes:192 (192.0 b)
          Interrupt:17 Memory:f0000000-f0012800 

eth5      Link encap:Ethernet  HWaddr 00:0F:53:07:50:7D  
          inet addr:90.206.66.13  Bcast:90.206.66.255  Mask:255.255.255.0
          inet6 addr: fe80::f:5300:107:507d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8590042795 errors:0 dropped:4 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:8308671 (7.9 MiB)  TX bytes:1132 (1.1 KiB)
          Interrupt:39 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:700 (700.0 b)  TX bytes:700 (700.0 b)

我有以下多播代码可供收听:

  socket_file_descriptor_ = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  int flag_on = 1;
  if ( ( setsockopt ( socket_file_descriptor_, SOL_SOCKET, SO_REUSEADDR, &flag_on,
                        sizeof(flag_on) ) ) < 0 )
      { 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);
  if ( ( setsockopt ( socket_file_descriptor_, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
                            (void*) &mc_req, sizeof(mc_req))) < 0) 
  { exit(1); } 

  {
    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 )
      { exit(1); }
  }
  // then recvfrom ( socket_file_descriptor_, _dest_, _len_, 0, NULL, NULL );

但这似乎并没有返回接口“eth5”上收到的数据。有什么指针吗?

1 个答案:

答案 0 :(得分:0)

您使用的是什么平台?我猜一个linux操作系统?

您是否完全确定消息到达您的界面(tcpdump,netcat?)。预期数据来自哪里 - 您是否也开发了“发件人”代码?它是否使用SO_BROADCAST选项?