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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c58b7bd180ea81b2,start X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!aioe.org!not-for-mail From: Dennis Hoppe Newsgroups: comp.lang.ada Subject: Problems with Ada.Streams.Read (blocking) Date: Fri, 08 Aug 2008 15:24:43 +0200 Organization: Aioe.org NNTP Server Message-ID: NNTP-Posting-Host: zWtj582HyEZs6irtsMNxbA.user.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Thunderbird 2.0.0.14 (Macintosh/20080421) Xref: g2news1.google.com comp.lang.ada:1544 Date: 2008-08-08T15:24:43+02:00 List-Id: Hi, I've written a minimal example to access an ftp server (like FileZilla). First, let's have a look at the code snippet: -- START with Ada.Text_IO; with Ada.Streams; with GNAT.Sockets; use GNAT.Sockets; use type Ada.Streams.Stream_Element_Count; procedure Test is Client: Socket_Type; Address: Sock_Addr_Type; Channel: Stream_Access; Data : Ada.Streams.Stream_Element_Array (1 .. 1); Offset : Ada.Streams.Stream_Element_Count; begin Initialize; Create_Socket(Client); Address.Addr := Inet_Addr("127.0.0.1"); Address.Port := 21; Connect_Socket (Client,Address); Channel := Stream(Client); loop -- reads in the welcome message Ada.Streams.Read (Channel.all, Data(1..1), Offset); exit when Offset = 0; -- alternative: exit when Offset /= Data'Last for I in 1 .. Offset loop Ada.Text_IO.Put (Character'Val (Data (I))); end loop; end loop; end Test; -- END The problem is, that Ada.Streams.Read is blocking, if the end of the stream is reached. I found many examples, that outline, that the variable Offset will be 0, if no further elements are on the stream. But this seems not to be the case, unfortunately. For a ftp server/client situation, each command is terminate by , so I enhanced the exit condition to: loop Ada.Streams.Read (Channel.all, Data(1..2), Offset); exit when (Character'Val (Data(1)) = ASCII.CR and Character'Val (Data(2)) = ASCII.LF); -- code omitted end loop; Of course, the Stream_Element_Array is enhanced to (1..2). This approach works very well, but some ftp commands send a messages over several lines. I do not know in advance, how many lines I should read in. Subsequently, Ada.Streams.Read has to be called in a loop, which will eventually block, again. How can I query the stream, if new elements are ready to read? Many thanks in advance, Dennis