comp.lang.ada
 help / color / mirror / Atom feed
From: Andrew Shvets <andrew.shvets@gmail.com>
Subject: Re: Finding the end of a stream from a socket
Date: Sat, 21 Oct 2017 12:18:49 -0700 (PDT)
Date: 2017-10-21T12:18:49-07:00	[thread overview]
Message-ID: <5e7770de-81b3-4815-9c43-8ef7118de05a@googlegroups.com> (raw)
In-Reply-To: <704c3b5d-413d-4a31-bc2b-44e42d26975d@googlegroups.com>

On Saturday, October 21, 2017 at 1:34:15 PM UTC-4, Andrew Shvets wrote:
> On Saturday, October 21, 2017 at 4:44:42 AM UTC-4, Dmitry A. Kazakov wrote:
> > On 2017-10-21 04:05, Andrew Shvets wrote:
> > 
> > > I've been recently trying to make a small TCP server (a toy.) I have
> > > a channel that reads Characters and was trying to find the end of
> > > the stream. Reading the documentation, I came across the following
> > > in  g-socket.ads:
> > > 
> > > type Stream_Access is access all Ada.Streams.Root_Stream_Type'Class;
> > > --  Same interface as Ada.Streams.Stream_IO
> > > 
> > > I was trying to have a way to find out when the last Character was 
> > > read from the stream. I could catch the Ada.IO_Exceptions.End_Error 
> > > exception, but I'm wondering if there is a better way.
> > 
> > End_Error is the best possible way, when applied. GNAT socket streams do 
> > not raise End_Error, AFAIK.
> > 
> > > while not Ada.Streams.Stream_IO.End_Of_File(Channel) loop
> > >    Character'Read(Channel, Received_Char);
> > >    Ada.Text_IO.Put(Received_Char);
> > > end loop;
> > 
> > This is a bad idea even when if it can work. In the case of sockets 
> > there is no file end. When the connection is closed by the peer the 
> > stream will stop both returning data and blocking. I suppose that will 
> > cause Constraint_Error in Character'Read.
> > 
> > In practice there is a higher level protocol on top of the socket 
> > stream, so that it is always known how many octets to read next.
> > 
> > Regarding your case, try this:
> > 
> >     Buffer : Stream_Element_Array (1..Buffer_Size);
> >     Last   : Stream_Element_Offset;
> > begin
> >     loop
> >        Receive_Socket (Socket, Buffer, Last);
> >        exit when Last < Buffer'First; -- Connection is closed by the peer
> >        for Octet in Buffer'First..Last loop -- Dump octets as-is
> >           Put (Character'Val (Buffer (Octet)));
> >        end loop;
> >     end loop;
> > 
> > -- 
> > Regards,
> > Dmitry A. Kazakov
> > http://www.dmitry-kazakov.de
> 
> Hi Dmitry,
> 
> This is my entire code.
> https://gist.github.com/anonymous/b7c3a5bdcd8c78453a643b6785573131

On this line:
GNAT.Sockets.Connect_Socket(Receiver,  Client_Addr);


I get the following exception:
!! TCP Server started !!
FOO 1

raised GNAT.SOCKETS.SOCKET_ERROR :[10022] Unknown system error


Weird, I thought, then I uncommented the Connect_Socket procedure call, since the socket is already being used to listen, it should be "connected".  When I did this, I got this error:

https://gist.github.com/anonymous/b79594f71309df87fc8fcc44bd6a28ee

What gets me, what is trying to connect from 0.0.0.0:0?  I have never seen or heard of this address being used.


      reply	other threads:[~2017-10-21 19:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-21  2:05 Finding the end of a stream from a socket Andrew Shvets
2017-10-21  8:44 ` Dmitry A. Kazakov
2017-10-21 13:45   ` Andrew Shvets
2017-10-21 18:11     ` Dennis Lee Bieber
2017-10-21 19:19       ` Dmitry A. Kazakov
2017-10-21 19:46         ` Andrew Shvets
2017-10-22 12:26         ` Andrew Shvets
2017-10-22 12:28           ` Andrew Shvets
2017-10-22 13:28           ` Dmitry A. Kazakov
2017-10-21 17:34   ` Andrew Shvets
2017-10-21 19:18     ` Andrew Shvets [this message]
replies disabled

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