3

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.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
eps_712
  • 135
  • 1
  • 2
  • 8
  • 1
    Expand on "doesn’t work". I don’t understand "using Wireshark to get the socket". – Simon Wright Nov 15 '16 at 17:18
  • I'm using Wireshark to capture all the packets that my net is receiving. As i don't receive anything on it when i execute the client i assume that it isn't sending it properly. – eps_712 Nov 16 '16 at 07:08

2 Answers2

4

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;
Eliot B.
  • 162
  • 11
  • In order to do the same thing for TCP, would I just need change Socket_Datagram (in client and server) to Socket_Stream? – gp443 Feb 21 '18 at 16:44
3

There are (at least) two problems with your program:

  1. You are mixing up UDP and TCP. UDP is not a stream-oriented protocol, so you shouldn't treat it as an Ada stream.
  2. You aren't setting up a connection with another machine.

Here is an example of a program communicating over UDP: https://bitbucket.org/sparre/udp-chat

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22