comp.lang.ada
 help / color / mirror / Atom feed
From: "mferracini" <maurizio.ferracini@gmail.com>
Subject: Re: Using Check_Selector ...
Date: 1 Mar 2005 03:25:12 -0800
Date: 2005-03-01T03:25:12-08:00	[thread overview]
Message-ID: <1109676312.473031.46240@f14g2000cwb.googlegroups.com> (raw)
In-Reply-To: mailman.157.1109674936.527.comp.lang.ada@ada-france.org

Duncan Sands wrote:

> You should do this "Set (Rset, ...)" every time around the loop (and
> clear the write set).  After all, if the selector times out the
> Rset will be empty, and then you loop around and check the
> selector with an empty Rset...

^_^ perfect now work fine, i post the source code for who have similar
problem.

Maurizio

-------------------------------------------------------------------------------
with Gnat.Sockets;use Gnat.Sockets;
with Ada.Exceptions;use Ada.Exceptions;
with Ada.Streams;
with Ada.Text_Io;

procedure server is
   Selector : Selector_Type;
   Rset,
   Wset     : Socket_Set_Type;
   Status   : Selector_Status;

   task Pong is
      entry Start;
   end Pong;

   task body Pong is
      Address : Sock_Addr_Type;
      Server  : Socket_Type;
      Socket  : Socket_Type;

   begin
      accept Start;

      Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
      Address.Port := 31337;

      Create_Socket (Server);
      Ada.Text_Io.Put_Line("In attesa di connessione");

      --  Allow reuse of local addresses.

      Set_Socket_Option
         (Server,
         Socket_Level,
         (Reuse_Address, True));

      Bind_Socket (Server, Address);

      Listen_Socket (Server);
      Empty(Rset);
      Empty(Wset);

      Accept_Socket (Server, Socket, Address);

      Ada.Text_Io.Put_Line ("nuova connessione");
      -- crea selector
      Set(Rset, Socket);
      Create_Selector(Selector);

      declare
         Message : Ada.Streams.Stream_Element_Array (1 .. 300);
         Last    : Ada.Streams.Stream_Element_Offset;

      begin
         loop
            Check_Selector(Selector, Rset, Wset, Status, 0.0);
            if (Status /= Expired ) then
               Receive_Socket(Socket,Message,Last);
               Ada.Text_Io.Put_Line("letto");

            else
               Ada.Text_Io.Put(Selector_Status'Image(Status)&" ");
            end if;
            Set(Rset, Socket);
         end loop;
      end;

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

begin

   Initialize (Process_Blocking_Io => False);
   Pong.Start;

end server;
--------------------------------------------------------------------------------
with Gnat.Sockets;use Gnat.Sockets;
with Ada.Streams;
with Ada.Text_Io;
with Ada.Integer_Text_Io;

procedure Client is
   Server : Sock_Addr_Type;
   Config : Ada.Text_Io.File_Type;

   Host       : String (1 .. 20) := (others => ' ');
   Port_Value : String (1 .. 20) := (others => ' ');
   Last       : Natural          := 0;
   Handle     : Socket_Type;

   Buffer  : Ada.Streams.Stream_Element_Array (1 .. 80);
   Sk_Last : Ada.Streams.Stream_Element_Offset;

begin
   --Inizializza
   --leggi da config
   Initialize(True); --true??

   Ada.Text_Io.Open(
      File => Config,
      Mode => Ada.Text_Io.In_File,
      Name => "config.ini"); --case sensitive?

   Ada.Text_Io.Get_Line(
      File => Config,
      Item => Host,
      Last => Last);

   Server.Addr := Addresses(Get_Host_By_Name( Host( 1 .. Last ) ) );

   Ada.Text_Io.Get_Line(
      File => Config,
      Item => Port_Value,
      Last => Last);

   Ada.Integer_Text_Io.Get(Port_Value,Natural(Server.Port),Last);

   Ada.Text_Io.Put_Line(Item => "TCP/IP Host: " & Host & "Porta:" &
Port_Type'Image (Server.Port));
   --apri la socket
   Create_Socket (Handle, Family_Inet, Socket_Stream);
   Set_Socket_Option(Handle,
      Socket_Level,
      (Reuse_Address, True));
   Connect_Socket (Handle, Server);
   Ada.Text_Io.Put_Line("Connesso");
   loop
      Buffer(1):=42;
      Send_Socket(Handle,Buffer,Sk_Last);
      Ada.Text_Io.Put_Line("->write something");
      delay(1.0);
   end loop;
end Client;
-------------------------------------------------------------------------------

the file config.ini is
--
localhost
31337
---




      reply	other threads:[~2005-03-01 11:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-01 10:49 Using Check_Selector mferracini
2005-03-01 11:01 ` Duncan Sands
2005-03-01 11:25   ` mferracini [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox