* Using Check_Selector ...
@ 2005-03-01 10:49 mferracini
2005-03-01 11:01 ` Duncan Sands
0 siblings, 1 reply; 3+ messages in thread
From: mferracini @ 2005-03-01 10:49 UTC (permalink / raw)
an other litte problem with non-blocking socket....
here the code
server:
-------------------------------
........
Accept_Socket (Server, Socket, Address);
Ada.Text_Io.Put_Line ("new connection");
Set(Rset, Socket);
Create_Selector(Selector);
begin
loop
Check_Selector(Selector, Rset, Wset, Status, 0.5);
if (Status /= Expired ) then
Receive_Socket(Socket,Message,Last);
Ada.Text_Io.Put_Line("read something");
else
Ada.Text_Io.Put(".");
end if;
end loop;
end;
-------------------------------
client:
-------------------------------
.........
loop
Buffer(1):=42;
Send_Socket(Handle,Buffer,Sk_Last);
Ada.Text_Io.Put_Line("OK");
delay(1.0);
end loop;
-------------------------------
the problem is : when programs start, the server read only the first
time.
---
new connection
read someting
............................................................
--
an hint?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Using Check_Selector ...
2005-03-01 10:49 Using Check_Selector mferracini
@ 2005-03-01 11:01 ` Duncan Sands
2005-03-01 11:25 ` mferracini
0 siblings, 1 reply; 3+ messages in thread
From: Duncan Sands @ 2005-03-01 11:01 UTC (permalink / raw)
To: mferracini; +Cc: comp.lang.ada
> Accept_Socket (Server, Socket, Address);
> Ada.Text_Io.Put_Line ("new connection");
> Set(Rset, Socket);
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...
> Create_Selector(Selector);
>
> begin
> loop
> Check_Selector(Selector, Rset, Wset, Status, 0.5);
> if (Status /= Expired ) then
> Receive_Socket(Socket,Message,Last);
> Ada.Text_Io.Put_Line("read something");
> else
> Ada.Text_Io.Put(".");
> end if;
> end loop;
> end;
All the best,
Duncan.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Using Check_Selector ...
2005-03-01 11:01 ` Duncan Sands
@ 2005-03-01 11:25 ` mferracini
0 siblings, 0 replies; 3+ messages in thread
From: mferracini @ 2005-03-01 11:25 UTC (permalink / raw)
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
---
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-03-01 11:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-01 10:49 Using Check_Selector mferracini
2005-03-01 11:01 ` Duncan Sands
2005-03-01 11:25 ` mferracini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox