使用GNAT.Sockets接收多播流量

时间:2019-09-29 00:12:26

标签: sockets windows-10 ada multicast gnat

我正在Ada中尝试IP多播,但似乎没有收到发送到多播组的任何流量。以某种方式,似乎我无法让应用程序获取传入的数据包。

我可以(使用Wireshark)验证从我的计算机发送了多播联接,也可以验证有数据正在发送到多播组。

我可以验证操作系统是否已通过netsh命令注册了多播连接:

netsh interfaces ip show joins

如果我运行程序,列出的组中的引用为1,否则为0。

以下过程显示了我的侦听器,我使用Mcast_IP => "239.255.128.128"Mcast_Port => "8807"调用了它:

with GNAT.Sockets;
with Ada.Streams;
with Ada.Text_IO;

   procedure Receive_Multicast (Mcast_IP   : in String;
                                Mcast_Port : in String)
   is
      package GS renames GNAT.Sockets;
      package AS renames Ada.Streams;
      package Tio renames Ada.Text_IO;

      use GS;

      use type Ada.Streams.Stream_Element_Offset;
      Socket  : GS.Socket_Type;
      Address : GS.Sock_Addr_Type;
      Data    : AS.Stream_Element_Array (1 .. 2**16);
      Offset  : AS.Stream_Element_Offset;
      Sender  : GS.Sock_Addr_Type;
   begin
      Address.Addr := Any_Inet_Addr;
      Address.Port := Port_Type'Value (Mcast_Port);

      Create_Socket (Socket => Socket,
                     Family => Family_Inet,
                     Mode   => Socket_Datagram);

      Bind_Socket (Socket, Address);

      --  Set socket options
      Set_Socket_Option (Socket,
                         Socket_Level,
                         (Reuse_Address, True));

      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Multicast_TTL, 1));

      Set_Socket_Option
        (Socket,
        IP_Protocol_For_IP_Level,
        (Multicast_Loop, True));

      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Add_Membership, Inet_Addr (Mcast_IP), Any_Inet_Addr));

      Tio.Put_Line ("Listening for MULTICASTS on port " & Address.Port'Img);

--  Receive the packet from the socket.
      loop
         Tio.Put_Line ("Waiting for incoming packets...");
         Receive_Socket (Socket => Socket,
                         Item   => Data,
                         Last   => Offset,
                         From   => Sender);
         Tio.Put_Line ("Received " & Offset'Img & " bytes.");
      end loop;
   end Receive_Multicast;

该过程一直进行到Receive_Socket调用(这是GNAT.Sockets包中的过程)。但是,即使我可以使用Wireshark确认多播流量,对Receive_Socket的调用仍会阻塞。

更新/解决方案:

上面的代码确实起作用了,尽管我必须完全卸载Kaspersky,这显然确实阻止了从我自己的计算机发送的多播的接收(即回送)。公认的答案也可以完美地工作。

1 个答案:

答案 0 :(得分:4)

基于GNAT.Sockets中的示例,以下代码应该可以工作。我删除了一些与接收无关的选项。

receive_multicast.ads

procedure Receive_Multicast 
  (IP_Address : String;
   Port       : String);

receive_multicast.adb

with Ada.Text_IO;
with Ada.Streams;
with GNAT.Sockets;

procedure Receive_Multicast 
  (IP_Address : String;
   Port       : String)
is

   use GNAT.Sockets;

   Address  : Sock_Addr_Type;
   Socket   : Socket_Type;

begin   

   Create_Socket (Socket, Family_Inet, Socket_Datagram);

   Set_Socket_Option
     (Socket => Socket,
      Level  => Socket_Level,
      Option => (Reuse_Address, True));

   Address.Addr := Any_Inet_Addr;
   Address.Port := Port_Type'Value (Port);

   Bind_Socket (Socket, Address);

   --  Join a multicast group

   --  Portability note: On Windows, this option may be set only
   --  on a bound socket.

   Set_Socket_Option
     (Socket => Socket,
      Level  => IP_Protocol_For_IP_Level,
      Option => (Add_Membership, Inet_Addr (IP_Address), Any_Inet_Addr));

   --  Receive the packet from the socket.
   declare

      use Ada.Text_IO;
      use Ada.Streams;

      Data    : Stream_Element_Array (1 .. 2**16);
      Offset  : Stream_Element_Offset;
      Sender  : Sock_Addr_Type;

   begin
      Put_Line ("Waiting for incoming packets...");

      Receive_Socket
        (Socket => Socket,
         Item   => Data,
         Last   => Offset,
         From   => Sender);

      Put_Line ("Received " & Offset'Image & " bytes.");
   end;

end Receive_Multicast;

main.adb

with Receive_Multicast;

procedure Main is
begin   
   Receive_Multicast 
     (IP_Address => "239.255.128.128",
      Port       => "8807");   
end Main;

我无法广泛测试代码,但是当我打开Windows PowerShell ISE时,加载并运行脚本Send-UdpDatagram.ps1(请参阅this GitHub Gist),然后执行:

PS C:\> Send-UdpDatagram -EndPoint "239.255.128.128" -Port 8807 -Message "testing"

然后Ada程序会回应:

Waiting for incoming packets...
Received  7 bytes.
[2019-09-29 10:55:58] process terminated successfully, elapsed time: 07.60s

更新

我还使用运行Raspbian GNU / Linux 10(破坏者)的Raspberry Pi测试了示例代码:

  • 在Raspberry Pi上安装了APT软件包gnatgprbuild
  • 将代码复制到Raspberry Pi。
  • 使用GNAT FSF(gprbuild -p <proj_name>.gpr)进行编译。
  • 启动了该程序的四个实例,每个实例都在单独的终端中。
  • 像以前一样使用PowerShell函数从Windows 10主机发出数据包。

结果是相同的:Raspberry Pi上的所有四个程序实例都接收到了数据包。在程序等待数据包时,我可以看到成员资格(另请参见this的帖子):

pi@raspberrypi:~ $ netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
[...]
eth0            4      239.255.128.128
[...]
pi@raspberrypi:~ $ netstat -anu | sort -nk4
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
[...]
udp        0      0 0.0.0.0:8807            0.0.0.0:*                          
udp        0      0 0.0.0.0:8807            0.0.0.0:*                          
udp        0      0 0.0.0.0:8807            0.0.0.0:*                          
udp        0      0 0.0.0.0:8807            0.0.0.0:*                          
[...]
相关问题