comp.lang.ada
 help / color / mirror / Atom feed
* Problems witth non-blocking TCP I/O
@ 2005-04-20 23:15 Frank Gerlach
  2005-04-21  8:37 ` Mark Lorenzen
  0 siblings, 1 reply; 2+ messages in thread
From: Frank Gerlach @ 2005-04-20 23:15 UTC (permalink / raw)


I am currently trying to write a TCP server using GNAT. When I test
this server from telnet, it blocks in Receive_Socket() until there are
bytes to read.
If I do the same with a small Ada program, it doesn't block any more.
Why is this ?
Here is a snippet of the server:

      Accept_Socket (Server, Socket, Address);

      for i in 1..10 loop      
        Receive_Socket (Socket,feldEA, LastValid, FromAddr);
        
        Put("read LastValid:");
        Put(Integer(LastValid));
        for j in 1..LastValid loop
           char:=CHARACTER'VAL(feldEA(j));
           Put(char);
        end loop;
        New_Line;
        
        Send_Socket (Socket,feldEA(1..LastValid), OctetsSent,
FromAddr);
        
      end loop;


This is my Ada client:

   Connect_Socket (Socket, Address);

   for i in 1..hs'LAST loop
      feldEA(Stream_Element_Offset(i) ):=CHARACTER'POS(hs(i)) ;
   end loop;   
   Send_Socket (Socket,feldEA(1..Stream_Element_Offset(hs'LAST)), 
                OctetsSent, FromAddr);
   Receive_Socket(Socket,feldEA, OctetsRead, FromAddr);
   Put("Read ");Put(INTEGER(OctetsRead));
   New_line;
   Close_Socket (Socket);

----------------------------------------------------------------------------
----------------------------------------------------------------------------
And here is the complete server Task:


with GNAT.Sockets; use GNAT.Sockets;

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Streams; use Ada.Streams;

package body PongPackage is

   task body Pong is
      Address  : Sock_Addr_Type;
      Server   : Socket_Type;
      Socket   : Socket_Type;
      Channel  : Stream_Access;
      
      type chararray is array(1..100) of Character;
      
      feld:chararray;
      x:integer;
      
      
      feldEA : Ada.Streams.Stream_Element_Array(1..100);
                 
      LastValid : Ada.Streams.Stream_Element_Offset;
      OctetsSent : Ada.Streams.Stream_Element_Offset;
      FromAddr : Sock_Addr_Type;
      
      --folgendes ist noetig um Zahlen via Put() auszugeben:
      package Int_IO is new Ada.Text_IO.Integer_IO(INTEGER);
      use Int_IO;

      --package modio is new
Ada.Text_IO.Modular_IO(Ada.Streams.Stream_Element);
      --use modio;
      
      char:Character;
      rt : Request_Type;

   begin
      Put_Line("start");
      accept Start;
      Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
      Put_Line("after Get_Host_By_Name");
      --  Get a socket address that is an Internet address and a port

      Address.Port := 5432;
      
      Create_Socket (Server);

      --  Allow reuse of local addresses.

      Set_Socket_Option
        (Server,
         Socket_Level,
         (Reuse_Address, True));
               
      Put_Line("after Set_Socket_Option");

      Bind_Socket (Server, Address);

      Listen_Socket (Server);

      --  Once a server calls Listen_Socket, incoming connects events
      --  can be accepted. The returned Socket is a new socket that
      --  represents the server side of the connection. Server remains
      --  available to receive further connections.

      Accept_Socket (Server, Socket, Address);

      for i in 1..10 loop      
        Receive_Socket (Socket,feldEA, LastValid, FromAddr);
        
        Put("read LastValid:");
        Put(Integer(LastValid));
        for j in 1..LastValid loop
           char:=CHARACTER'VAL(feldEA(j));
           Put(char);
        end loop;
        New_Line;
        
        Send_Socket (Socket,feldEA(1..LastValid), OctetsSent,
FromAddr);
        
      end loop;
      
      Close_Socket (Server);
      Close_Socket (Socket);
      accept Stop;
   end Pong;


end PongPackage;



----------------------------------------------------------------------------
----------------------------------------------------------------------------

Complete Client code:


with GNAT.Sockets; use GNAT.Sockets;

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Streams; use Ada.Streams;


procedure Client is


task type Ping is
     entry Start;
     entry Stop;
end Ping;

task body Ping is
   package modio is new
Ada.Text_IO.Modular_IO(Ada.Streams.Stream_Element);
      use modio;
   package Int_IO is new Ada.Text_IO.Integer_IO(INTEGER);
      use Int_IO;

   Address  : Sock_Addr_Type;
   Socket   : Socket_Type;
   Channel  : Stream_Access;
   
   OctetsSent : Ada.Streams.Stream_Element_Offset;
   OctetsRead : Ada.Streams.Stream_Element_Offset;
   FromAddr : Sock_Addr_Type;
   feldEA : Ada.Streams.Stream_Element_Array(1..100);
   hs: String:="Hallo Duda";
begin
   accept Start;

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

   Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
   Address.Port := 5432;
   Create_Socket (Socket);

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

   Connect_Socket (Socket, Address);

   for i in 1..hs'LAST loop
      feldEA(Stream_Element_Offset(i) ):=CHARACTER'POS(hs(i)) ;
   end loop;   
   Send_Socket (Socket,feldEA(1..Stream_Element_Offset(hs'LAST)),
OctetsSent, FromAddr);
   Receive_Socket(Socket,feldEA, OctetsRead, FromAddr);
   Put("Read ");Put(INTEGER(OctetsRead));
   New_line;
   Close_Socket (Socket);
   accept Stop;
end Ping;




Pinger : Ping;

begin --procedure Client
  GNAT.Sockets.Initialize (Process_Blocking_IO => False);
  Pinger.Start;
  Pinger.Stop;
end Client;



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Problems witth non-blocking TCP I/O
  2005-04-20 23:15 Problems witth non-blocking TCP I/O Frank Gerlach
@ 2005-04-21  8:37 ` Mark Lorenzen
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Lorenzen @ 2005-04-21  8:37 UTC (permalink / raw)


frankgerlach@gmail.com (Frank Gerlach) writes:

> I am currently trying to write a TCP server using GNAT. When I test
> this server from telnet, it blocks in Receive_Socket() until there are
> bytes to read.
> If I do the same with a small Ada program, it doesn't block any more.
> Why is this ?

<snip a lot of code>

If you are running on Linux, try to "strace" the server and see which
system calls are actually performed.

- Mark Lorenzen



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-04-21  8:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-20 23:15 Problems witth non-blocking TCP I/O Frank Gerlach
2005-04-21  8:37 ` Mark Lorenzen

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