Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create an UDP client in Ada

I'm working on UDP communications using Ada. This code has to send some data to another host which is going to process it. I'm trying to send an initial message to start the communication, but it doesn't work. My client code is the following:

with GNAT.Sockets; 
use GNAT.Sockets;
with Ada.Text_IO;
with Ada.Exceptions; 
use Ada.Exceptions;
procedure Client_Send is

 task Send is
    entry Start;
    entry Stop;
 end Send;

 task body Send is
    Address  : Sock_Addr_Type;
    Socket   : Socket_Type;
    Channel  : Stream_Access;

 begin
    accept Start;

    --  See comments in Ping section for the first steps.

    Address.Addr := Inet_Addr( "192.168.0.1" );
    Address.Port := 7777;
    Create_Socket (Socket,Family_Inet,Socket_Datagram);
    Bind_Socket (Socket, Address);

    Channel := Stream (Socket);

    String'Output (Channel, "Hello world");
    Free(Channel);

    Ada.Text_IO.Put_Line ("Mesnaje Enviado");
    Close_Socket (Socket);
    accept Stop;

 exception when E : others =>
   Ada.Text_IO.Put_Line
      (Exception_Name (E) & ": " & Exception_Message (E));
 end Send;

  begin
     Initialize (Process_Blocking_IO => False);
     Send.Start;
     Send.Stop;
     Finalize;
  end Client_Send;

I'm using Wireshark to view the inbound traffic, but it doesn't receive anything.

like image 674
eps_712 Avatar asked Jan 16 '26 23:01

eps_712


1 Answers

Here is a simple UDP Client / Server in Ada with GNAT Sockets :

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;
like image 125
Eliot B. Avatar answered Jan 19 '26 20:01

Eliot B.