1

I have to receive UDP datagrams from a C++ client. The Client works correctly and send a datagram to the ip where i compile this code. Datagram's size is 800Bytes. When i send the datagram, the read process gets blocked. I have created this code following this example: https://en.wikibooks.org/wiki/Ada_Programming/Libraries/GNAT.Sockets_examples, and this works perfectly on my host. Any idea?

procedure RECEIVE_DATA (
                         DEST_UDP_PORT        : In  WORD_TYPE;
                         SRC_UDP_PORT         : In  WORD_TYPE;
                         WAIT_TIME       : In  DURATION;
                         MESSAGE_ADDRESS : Out System.Address;
                         WAIT_RESULT     : Out Boolean
                         ) is

task Receive is
    entry Start;
end Receive;

task body Receive is
    Address  : Sock_Addr_Type;
    Socket   : Socket_Type;
    Channel  : Stream_Access;
    Receive_Timeout : constant Duration := WAIT_TIME;
    Offset : Ada.Streams.Stream_Element_Count;
    Data   : Ada.Streams.Stream_Element_Array (1 .. 800);
begin
    accept Start;
    Initialize;
    WAIT_RESULT := False;       
    Create_Socket (Socket, Family_Inet, Socket_Datagram);
    Set_Socket_Option (Socket => Socket,
                       Option => (Gnat.Sockets.Receive_Timeout, Timeout => Receive_Timeout));

    Address.Addr := Inet_Addr(DEST_UDP_IP);
    Address.Port := Port_Type(DEST_UDP_PORT);
    Bind_Socket (Socket, Address);

    Address.Addr := Inet_Addr(SRC_UDP_IP);
    Address.Port := Port_Type(SRC_UDP_PORT);
    Channel := Stream (Socket, Address);

    loop
        Ada.Streams.Read (Channel.All, Data, Offset);
        exit when Offset = 0;
    end loop;
    Free (Channel);
    Close_Socket (Socket);
    WAIT_RESULT := True;
    MESSAGE_ADDRESS := Data'Address;

    exception when E : others =>
        Ada.Text_IO.Put_Line
        (Exception_Name (E) & ": " & Exception_Message (E));
         WAIT_RESULT := False;
         Free (Channel);
         Close_Socket (Socket);
end Receive;

begin
    Initialize (Process_Blocking_IO => False);
    Receive.Start;
    Finalize;
end RECEIVE_DATA;
eps_712
  • 135
  • 1
  • 2
  • 8

1 Answers1

3

Have you tried using GNAT.Sockets.Receive_Socket ? Streams work with TCP, you cannot stream datagrams.

Example:

Client:

with Ada.Streams;
with Ada.Text_IO;

with GNAT.Sockets;

procedure UDP_Client is
   use GNAT.Sockets;
   Address : Sock_Addr_Type;
   Socket : Socket_Type;
   Data : constant Ada.Streams.Stream_Element_Array (1 .. 512) := (others => 42);
   Last : Ada.Streams.Stream_Element_Offset;
begin
   Address.Port := 50001;
   Address.Addr := Inet_Addr ("127.0.0.1");
   Create_Socket (Socket, Family_Inet, Socket_Datagram);
   Send_Socket (Socket, Data, Last, Address);
   Ada.Text_IO.Put_Line ("last :" & Last'Img);
end UDP_Client;

Server :

with Ada.Streams;
with Ada.Text_IO;

with GNAT.Sockets;

procedure UDP_Server is
   use GNAT.Sockets;
   Server : Socket_Type;
   Address, From : Sock_Addr_Type;
   Data : Ada.Streams.Stream_Element_Array (1 .. 512);
   Last : Ada.Streams.Stream_Element_Offset;
   Watchdog : Natural := 0;
begin
   Create_Socket (Server, Family_Inet, Socket_Datagram);
   Set_Socket_Option
     (Server,
      Socket_Level,
      (Reuse_Address, True));
   Set_Socket_Option
     (Server,
      Socket_Level,
      (Receive_Timeout,
       Timeout => 1.0));
   Address.Addr := Any_Inet_Addr;
   Address.Port := 50001;
   Bind_Socket (Server, Address);
   loop
      begin
         GNAT.Sockets.Receive_Socket (Server, Data, Last, From);
         Ada.Text_IO.Put_Line ("last : " & Last'Img);
         Ada.Text_IO.Put_Line ("from : " & Image (From.Addr));
      exception
         when Socket_Error =>
            Watchdog := Watchdog + 1;
            exit when Watchdog = 10;
      end;
   end loop;
end UDP_Server;
Eliot B.
  • 162
  • 11
  • GNAT.Sockets.Receive_Socket gets blocked just like read process. – eps_712 Nov 23 '16 at 11:32
  • Here's an exemple of a simple UDP client / server : – Eliot B. Nov 23 '16 at 12:20
  • Yes. In Berkley sockets lingo, "Stream" = TCP and "Datagram" = UDP. – T.E.D. Nov 23 '16 at 12:51
  • I would emphasize on the part of the code that makes the send/receive via UDP in a non blocking fashion using GNAT sockets. I.e. : `Set_Socket_Option (My_Socket, Socket_Level, (Receive_Timeout, Timeout => 1.0));` As the code provided by @eliot-b also points out, you will need to catch the Socket_Error exception and handle it, since this exception is raised when timeout is reached. If you want to handle other socket_error codes, you'll need further coding. See for instance https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx for error codes in Windows. – LoneWanderer Mar 27 '17 at 21:49