comp.lang.ada
 help / color / mirror / Atom feed
From: Jere <jhb.chat@gmail.com>
Subject: Re: Ada x <whatever> Datagram Sockets
Date: Thu, 7 Feb 2019 03:47:12 -0800 (PST)
Date: 2019-02-07T03:47:12-08:00	[thread overview]
Message-ID: <519fd5e0-eb4e-432e-85cc-d7a37510c957@googlegroups.com> (raw)
In-Reply-To: <e51edaf8-b82e-4f75-86eb-168b43f67a18@googlegroups.com>

On Thursday, February 7, 2019 at 1:41:28 AM UTC-5, Rego, P. wrote:
> On Thursday, February 7, 2019 at 4:00:11 AM UTC-2, Egil H H wrote:
> > > > On Wednesday, February 6, 2019 at 6:10:37 PM UTC-5, Rego, P. wrote:
> > > 
> > > Just the string "test". I also confirmed that the MQL container (uchar data[]) has really the size 5.
> > > 
> > 
> > Right, Ada strings are not zero-terminated, and the size of the string needs 
> > to come from somewhere, which is why String'Input will interpret the first x bits (probably 32, but may depend on your platform) as the length. So in this
> > case, "test" becomes a big number and 'Input will most likely try to allocate
> > just under 2GB  on your stack (and probably at least twice, in order to copy
> > the result into your string).
> A C++ uchar in Windows machine is usually 1 byte, so assuming that both use the same signature, both messages (from C++ and from Ada) should be of same size, right? 
> 
The problem is you are telling it to send it one way and receive it
in another way.  In C++ you tell the code to send:
't' 'e' 's' 't' '\0'

In Ada you are telling it to receive:

<length byte 1..4> <data byte 1..length>

if it reads your c++ packet in that format, assuming a little endian 
system:

<0x74736574> <\0 + random junk>

That translates to specifying a length of 1953719668 bytes, which is 
above the 65507 limit.

For giggles try modifying your receive code a bit:

      declare
         subtype Test_String is String(1..5);
         Message : Test_String := Test_String'Input (Channel);
      begin
         Address := SOCKETS.Get_Address (Channel);
         Text_IO.Put_Line (Message & " from " & SOCKETS.Image (Address));
         Test_String'Output (Channel, Message);
      end; 

I don't recall if C actually sends the '\0' at the end or not
for sockets, so if it doesn't, then change the Test_String declaration
to

      subtype Test_String is String(1..4);

However, I think it does send the '\0', so you will also have to
handle that in your Ada code (since strings don't terminate with a
'\0' in Ada).

Changing String to a constrained subtype might change the pattern
to be closer to the C++ pattern sent.  This doesn't solve your problem
because you never know how long the data is for real data, but would
at least (hopefully) get you talking.

> 
> > 
> > Don't use Ada stream attributes to communicate with other languages.
> > 
> > And, as some will tell you, don't use them at all, especially for arrays, as
> > each array element may end up being transferred as a single data packet, destroying performance.
> 
> This is quite controversial, in this case. I'd usually agree with you, however the use of Ada stream is not by choice. The whole GNAT Ada package uses Ada streams, and actually the above code was entirely extracted from the comments from g-sockets.ads with minimum modification. I don't think it would be wise to re-implement Ada Sockets pkg to be more efficient.

I've never used streams with the GNAT socket package for production code,
so I cannot comment on them, but the package definitely offers other
options besides streams.  I generally have a task that periodically
reads up to a specific number of bytes into a buffer and then my other
tasks can handle the data as they see fit.  You can even have the OS
tell you how many bytes are available to read before you do.

  parent reply	other threads:[~2019-02-07 11:47 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-06 23:10 Ada x <whatever> Datagram Sockets Rego, P.
2019-02-07  0:42 ` Jere
2019-02-07  5:28   ` Rego, P.
2019-02-07  6:00     ` Egil H H
2019-02-07  6:41       ` Rego, P.
2019-02-07  7:23         ` Egil H H
2019-02-07 11:48           ` Jere
2019-02-08 19:41           ` Rego, P.
2019-02-08 20:31             ` Dmitry A. Kazakov
2019-02-08 21:56               ` Rego, P.
2019-02-07  8:28         ` Dmitry A. Kazakov
2019-02-07 10:08           ` Simon Wright
2019-02-08  0:15           ` Randy Brukardt
2019-02-08  8:25             ` Simon Wright
2019-02-08 13:24               ` Dmitry A. Kazakov
2019-02-09  1:01               ` Randy Brukardt
2019-02-10 17:54                 ` Simon Wright
2019-02-11  8:39                   ` Dmitry A. Kazakov
2019-02-11 13:35                     ` Simon Wright
2019-02-11 14:25                       ` Dmitry A. Kazakov
2019-02-11 15:19                         ` Simon Wright
2019-02-11 16:04                           ` Dmitry A. Kazakov
2019-02-11 23:19                   ` Randy Brukardt
2019-02-12 11:35                     ` Simon Wright
2019-02-08 19:44           ` Rego, P.
2019-02-07 11:47         ` Jere [this message]
2019-02-07 18:00           ` Jeffrey R. Carter
2019-02-08 20:35             ` Rego, P.
2019-02-08 21:26               ` Jeffrey R. Carter
2019-02-08 22:02                 ` Rego, P.
2019-02-08 21:38               ` Dmitry A. Kazakov
2019-02-08 20:00           ` Rego, P.
2019-02-07 10:11     ` Simon Wright
2019-02-08 20:03       ` Rego, P.
replies disabled

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