From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,eec9eaa7ef85aa12,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!not-for-mail From: frankgerlach@gmail.com (Frank Gerlach) Newsgroups: comp.lang.ada Subject: Problems witth non-blocking TCP I/O Date: 20 Apr 2005 16:15:29 -0700 Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: 84.160.227.129 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1114038930 5526 127.0.0.1 (20 Apr 2005 23:15:30 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 20 Apr 2005 23:15:30 +0000 (UTC) Xref: g2news1.google.com comp.lang.ada:10619 Date: 2005-04-20T16:15:29-07:00 List-Id: 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;