From: frankgerlach@gmail.com (Frank Gerlach)
Subject: Problems witth non-blocking TCP I/O
Date: 20 Apr 2005 16:15:29 -0700
Date: 2005-04-20T16:15:29-07:00 [thread overview]
Message-ID: <d18a5f0d.0504201515.6af1bc2c@posting.google.com> (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;
next reply other threads:[~2005-04-20 23:15 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-04-20 23:15 Frank Gerlach [this message]
2005-04-21 8:37 ` Problems witth non-blocking TCP I/O Mark Lorenzen
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox