comp.lang.ada
 help / color / mirror / Atom feed
* Re: Get_Line vs Adasockets
  2000-08-13  0:00 Get_Line vs Adasockets Ultor
@ 2000-08-13  0:00 ` Dale Stanbrough
  2000-08-14  0:00   ` Martin Dowie
  2000-08-14  0:00 ` Ken Garlington
  2000-08-14  0:00 ` Anders Gidenstam
  2 siblings, 1 reply; 12+ messages in thread
From: Dale Stanbrough @ 2000-08-13  0:00 UTC (permalink / raw)


In article <8n6cbc$9ge$1@news.tpi.pl>, "Ultor" <ultor@hert.org> wrote:

> Hello
> 
> I'm new in ADA (code since Friday). I got a problem with Get_Line in my
> source. I tried to make it working over 2 hours and still got a problem 
> with
> that.
> 
> When i did Get_Line like this
> 
>    BUF := Get_Line(Incoming_Socket);
> 
> then I had problem with CONSTRAINT_ERROR. I don't know how to set size of
> BUF to be the same as line from Get_Line (as I know that should eliminate
> CONSTRAINT_ERROR).
> 
> I would be very gratefull if some1 could correct my source.

if you assign one array to another, and they are not the same length you 
get a constraint error.



[stuff deleted]
> 
>         Get_Line(Incoming_Socket,Buf,Buf'Size);
>         if BUF(1..Buf'Length)="QUIT"


this will -never- work because a 500 character string can never equal a 
4 char string.

try  
   if Buf (1..4) = "QUIT" then





...and i can't see a return statement in your function.


Dale




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Get_Line vs Adasockets
@ 2000-08-13  0:00 Ultor
  2000-08-13  0:00 ` Dale Stanbrough
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Ultor @ 2000-08-13  0:00 UTC (permalink / raw)


Hello

I'm new in ADA (code since Friday). I got a problem with Get_Line in my
source. I tried to make it working over 2 hours and still got a problem with
that.

When i did Get_Line like this

   BUF := Get_Line(Incoming_Socket);

then I had problem with CONSTRAINT_ERROR. I don't know how to set size of
BUF to be the same as line from Get_Line (as I know that should eliminate
CONSTRAINT_ERROR).

I would be very gratefull if some1 could correct my source.

---- snip -------
with Ada.Exceptions,Ada.Text_IO,Sockets,Sockets.Thin,Sockets.Naming;
use Ada.Exceptions,Ada.Text_IO,Sockets,Sockets.Thin,Sockets.Naming;
with Ada.Unchecked_Conversion; use type C.int;

procedure testsock is
  function Sock2In is new Ada.Unchecked_Conversion (Source =>
Sockaddr_Access, Target => Sockaddr_In_Access);
  Accepting_Socket : Socket_FD;
  Incoming_Socket  : Socket_FD;
  Socker           : Sockaddr_Access := new Sockaddr;
  BUF              : String (1..500);
  i                : aliased C.int;

begin
  Socket (Accepting_Socket, AF_INET, SOCK_STREAM);
  Setsockopt (Accepting_Socket, SOL_SOCKET, SO_REUSEADDR, 1);
  Bind (Accepting_Socket, 31337);
  Listen (Accepting_Socket);
  Accept_Socket (Accepting_Socket, Incoming_Socket);

  loop
        i:=C_Getpeername(Get_FD(Incoming_Socket),Socker,i'Access);
        Put_Line(Incoming_Socket,"CONNECTED FROM: " &
Image(Sock2In(Socker).Sin_Addr));

        Get_Line(Incoming_Socket,Buf,Buf'Size);
        if BUF(1..Buf'Length)="QUIT" then Put_Line (Incoming_Socket, "BYE
BYE"); end if;

        Shutdown(Incoming_Socket,Both);
  end loop;

end testsock;
---- snip ----

PS. Where can i get some adasockets example sources (other than this one
from library) ?

Best Regards,

 Marek Bialoglowy [Ultor@hert.org] ------ Network Security Consultant
 GROUP: HERT (www.hert.org) -- PGP: http://www.hert.org/pgp/Ultor.asc






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-13  0:00 Get_Line vs Adasockets Ultor
  2000-08-13  0:00 ` Dale Stanbrough
@ 2000-08-14  0:00 ` Ken Garlington
  2000-08-14  0:00   ` Ultor
  2000-08-21  1:44   ` David Thompson
  2000-08-14  0:00 ` Anders Gidenstam
  2 siblings, 2 replies; 12+ messages in thread
From: Ken Garlington @ 2000-08-14  0:00 UTC (permalink / raw)


"Ultor" <ultor@hert.org> wrote in message news:8n6cbc$9ge$1@news.tpi.pl...
> Hello
>
> I'm new in ADA (code since Friday). I got a problem with Get_Line in my
> source. I tried to make it working over 2 hours and still got a problem
with
> that.
>
> When i did Get_Line like this
>
>    BUF := Get_Line(Incoming_Socket);
>
> then I had problem with CONSTRAINT_ERROR.

Assuming that you expect the client to send a string terminated with
<CR><LF>, as opposed to a fixed-length message with no specific terminator
(which are both possible with socket programming), you've probably got the
right general idea in using Get_Line. However, you've come across an
important difference between C and Ada. In C, you would do a read like this:

   read(incoming_socket, buf, sizeof(buf));

where "buf" is the buffer you've allocated, and sizeof(buf) is the maximum
size of the string you'll accept. You have to pass the size of the buffer
separately, since in C the size of an array is not intrinsically available
to the called program. To find out the length of the string that was
actually passed, you would do something like

   current_string_length = strlen(buf)

which would determine the size of the string based on where the null
terminator is located.

 The equivalent in Ada is

   Get_Line(incoming_socket, buf, current_string_length);

Here, you don't have to pass buf'size as an *input* to get_line, since
that's automatically available to Get_Line just by making buf a parameter.
On the other hand, Ada strings are not null-terminated, so you need an
*output* value that says how many characters were actually read (<= 500).
That's why you're getting the error: the third parameter of Get_Line in this
case is "Last : out Natural"... it looks like you're trying to use it as an
*in* parameter.

> procedure testsock is ...
>   BUF              : String (1..500);
     Current_String_Length : Natural;

> begin...
>   loop...
         Get_Line(Incoming_Socket, Buf, Current_String_Length);
         if BUF(1..Current_String_Length)="QUIT" ...






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-13  0:00 ` Dale Stanbrough
@ 2000-08-14  0:00   ` Martin Dowie
  2000-08-17  0:00     ` tmoran
  0 siblings, 1 reply; 12+ messages in thread
From: Martin Dowie @ 2000-08-14  0:00 UTC (permalink / raw)


Yeah, I thought that too for a minute, but on closer inspection, the code is a
procedure with a nested function (instance of unchecked_conversion)  and not a
package with a function being defined in it - so no 'return' statement required
(although allowed ;-).

Perhaps one of the language lawyers could comment on the use of
unchecked_conversion to (I assume) convert from an address to an access type? I
thought that was the purpose of the standard package
System.Address_To_Access_Conversions?

Dale Stanbrough wrote:
> 
> 
> ...and i can't see a return statement in your function.
> 
> Dale

-- 
The views expressed here are personal and not those of BAE Systems.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-14  0:00   ` Ultor
@ 2000-08-14  0:00     ` Ted Dennison
  2000-08-14  0:00       ` Michal Zalewski
  0 siblings, 1 reply; 12+ messages in thread
From: Ted Dennison @ 2000-08-14  0:00 UTC (permalink / raw)


In article <8n8lku$2ks$1@news.tpi.pl>,
  "Ultor" <ultor@hert.org> wrote:
> > > procedure testsock is ...
> > >   BUF              : String (1..500);
> >      Current_String_Length : Natural;
> >
> > > begin...
> > >   loop...
> >          Get_Line(Incoming_Socket, Buf, Current_String_Length);
> >          if BUF(1..Current_String_Length)="QUIT" ...
>
> That won't work cause Incoming_Socket is not File_Type ... will work
> if ther's some way to convert Incoming_Socket to File_Type.


Well, the "File" parameter has to be of Ada.Text_IO.File_Type, of
course. If its not, you are going to have to do something else besides
Text_IO.

What exactly is "Incoming_Socket", and where did you get it?

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-14  0:00       ` Michal Zalewski
@ 2000-08-14  0:00         ` Ted Dennison
  0 siblings, 0 replies; 12+ messages in thread
From: Ted Dennison @ 2000-08-14  0:00 UTC (permalink / raw)


In article <Pine.LNX.4.21.0008141730540.13077-100000@dione.ids.pl>,
  Michal Zalewski <lcamtuf@dione.ids.pl> wrote:
> On Mon, 14 Aug 2000, Ted Dennison wrote:
>
> > > That won't work cause Incoming_Socket is not File_Type ... will
work
> > > if ther's some way to convert Incoming_Socket to File_Type.
> >
> >
> > Well, the "File" parameter has to be of Ada.Text_IO.File_Type, of
> > course. If its not, you are going to have to do something else
besides
> > Text_IO.
>
> Well, function Get_Line is overloaded within adasockets package (it's
nice
> binding, at least for GNAT, available at
http://www.infres.enst.fr/ANC/).
> Unfortunately, author didn't overloaded Get_Line(socket,string,out
> natural), but only Get_Line(socket). So, in case described by Marek,
> Get_Line from Ada.Text_IO was called, resulting in type mismatch. His
> second attempt caused array size mismatch (because Get_Line(socket)
return
> buffer length may vary).
>
> One - obvious - solution is calling Get_Line at declaration time (just
> like someone suggested here), other solution are: converting Socket to
> Ada.Text_IO.File_Type or writing own Get_Line for sockets.

Ahhh. So I presume this version of Get_Line is a function that returns a
string? In that case, another option would be to pass its string value
straight into Ada.Strings.Unbounded.To_Unbounded_String. eg:

      Line : Ada.Strings.Unbounded.Unbounded_String;
   begin
      ...
      Line := Ada.Strings.Unbounded.To_Unbounded_String
         (Get_Line (Socket));

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-13  0:00 Get_Line vs Adasockets Ultor
  2000-08-13  0:00 ` Dale Stanbrough
  2000-08-14  0:00 ` Ken Garlington
@ 2000-08-14  0:00 ` Anders Gidenstam
  2 siblings, 0 replies; 12+ messages in thread
From: Anders Gidenstam @ 2000-08-14  0:00 UTC (permalink / raw)


In article <8n6cbc$9ge$1@news.tpi.pl>,
	"Ultor" <ultor@hert.org> writes:
>Hello
>
>I'm new in ADA (code since Friday). I got a problem with Get_Line in my
>source. I tried to make it working over 2 hours and still got a problem with
>that.
>
>When i did Get_Line like this
>
>   BUF := Get_Line(Incoming_Socket);
>
>then I had problem with CONSTRAINT_ERROR. I don't know how to set size of
>BUF to be the same as line from Get_Line (as I know that should eliminate
>CONSTRAINT_ERROR).

Hello!

Well, that statement will only work if BUF and the string Get_Line returns both
have the same length...
One way to solve this problem is to use a declare-block, ie not declaring BUF
until you know how large it needs to be:

declare
  BUF : String := Get_Line(Incoming_Socket);
begin
  -- Do stuff that uses BUF.

end;

Another way is to process the data in a procedure or function:

procedure Do_Stuff (BUF : String) is ...

And in your program you call it with Get_Line as a parameter:

...
Do_Stuff (BUF => Get_Line (Incoming_Socket));
...

Regards
Anders
-- 
"A well-written program is its own heaven; 
 a poorly-written program is its own hell."
  - The Tao of Programming 





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-14  0:00 ` Ken Garlington
@ 2000-08-14  0:00   ` Ultor
  2000-08-14  0:00     ` Ted Dennison
  2000-08-21  1:44   ` David Thompson
  1 sibling, 1 reply; 12+ messages in thread
From: Ultor @ 2000-08-14  0:00 UTC (permalink / raw)


Hello

> > procedure testsock is ...
> >   BUF              : String (1..500);
>      Current_String_Length : Natural;
>
> > begin...
> >   loop...
>          Get_Line(Incoming_Socket, Buf, Current_String_Length);
>          if BUF(1..Current_String_Length)="QUIT" ...

That won't work cause Incoming_Socket is not File_Type ... will work if
ther's some way to convert Incoming_Socket to File_Type.

PS:  I'm using GNAT.

--
Best Regards,

 Marek Bialoglowy [Ultor@hert.org] ------ Network Security Consultant
 GROUP: HERT (www.hert.org) -- PGP: http://www.hert.org/pgp/Ultor.asc






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-14  0:00     ` Ted Dennison
@ 2000-08-14  0:00       ` Michal Zalewski
  2000-08-14  0:00         ` Ted Dennison
  0 siblings, 1 reply; 12+ messages in thread
From: Michal Zalewski @ 2000-08-14  0:00 UTC (permalink / raw)


On Mon, 14 Aug 2000, Ted Dennison wrote:

> > That won't work cause Incoming_Socket is not File_Type ... will work
> > if ther's some way to convert Incoming_Socket to File_Type.
> 
> 
> Well, the "File" parameter has to be of Ada.Text_IO.File_Type, of
> course. If its not, you are going to have to do something else besides
> Text_IO.

Well, function Get_Line is overloaded within adasockets package (it's nice
binding, at least for GNAT, available at http://www.infres.enst.fr/ANC/).
Unfortunately, author didn't overloaded Get_Line(socket,string,out
natural), but only Get_Line(socket). So, in case described by Marek,
Get_Line from Ada.Text_IO was called, resulting in type mismatch. His
second attempt caused array size mismatch (because Get_Line(socket) return
buffer length may vary).

One - obvious - solution is calling Get_Line at declaration time (just
like someone suggested here), other solution are: converting Socket to
Ada.Text_IO.File_Type or writing own Get_Line for sockets.

Regards,
_______________________________________________________
Michal Zalewski [lcamtuf@tpi.pl] [tp.internet/security]
[http://lcamtuf.na.export.pl] <=--=> bash$ :(){ :|:&};:
=-----=> God is real, unless declared integer. <=-----=





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-14  0:00   ` Martin Dowie
@ 2000-08-17  0:00     ` tmoran
  2000-08-21  0:00       ` Martin Dowie
  0 siblings, 1 reply; 12+ messages in thread
From: tmoran @ 2000-08-17  0:00 UTC (permalink / raw)


>Perhaps one of the language lawyers could comment on the use of
>unchecked_conversion to (I assume) convert from an address to an access type?
  It's not required to do what you want, but often does.

>I thought that was the purpose of the standard package
>System.Address_To_Access_Conversions?
  Yes.  That is supposed to do what you want.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-17  0:00     ` tmoran
@ 2000-08-21  0:00       ` Martin Dowie
  0 siblings, 0 replies; 12+ messages in thread
From: Martin Dowie @ 2000-08-21  0:00 UTC (permalink / raw)


indeed, i've never come across a compiler that didn't do this, but
shouldn't we be writing to avoid as many portability issues as
possible? it seems just as easy to use this standard generic as
it does to use unchecked_conversion and we get the portability as
a freebie.

tmoran@bix.com wrote:
> 
> >Perhaps one of the language lawyers could comment on the use of
> >unchecked_conversion to (I assume) convert from an address to an access type?
>   It's not required to do what you want, but often does.
> 
> >I thought that was the purpose of the standard package
> >System.Address_To_Access_Conversions?
>   Yes.  That is supposed to do what you want.

-- 
The views expressed here are personal and not those of BAE Systems.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Get_Line vs Adasockets
  2000-08-14  0:00 ` Ken Garlington
  2000-08-14  0:00   ` Ultor
@ 2000-08-21  1:44   ` David Thompson
  1 sibling, 0 replies; 12+ messages in thread
From: David Thompson @ 2000-08-21  1:44 UTC (permalink / raw)


Ken Garlington <Ken.Garlington@computer.org> wrote :
...
> important difference between C and Ada. In C, you would do a read like this:
>
>    read(incoming_socket, buf, sizeof(buf));
>
Actually result_len_or_err_flag = read ...;
Yes, combining both in one return value is ugly.

> where "buf" is the buffer you've allocated, and sizeof(buf) is the maximum
> size of the string you'll accept. You have to pass the size of the buffer
> separately, since in C the size of an array is not intrinsically available
> to the called program.

Yes.

> ... To find out the length of the string that was
> actually passed, you would do something like
>
>    current_string_length = strlen(buf)
>
> which would determine the size of the string based on where the null
> terminator is located.
>
No.  (I assume you mean actually received/"returned".)
But read() (or recv() or other similar OS calls) does *not*
add the null terminator needed to ensure a valid C string,
plus it is perfectly valid (to the OS and socket stack)
for the data received to *include* null data characters.
(Whether it is valid for the protocol you are implementing,
e.g. HTTP SMTP FTP etc., is your problem.)

You use the value returned from read(), if > 0.
If < 0 (canonically -1), an error occurred; if = 0,
you hit end of file (which for an incoming TCP socket
means the peer entity has done a TCP shutdown).
You must check; no exceptions are raised.

If you want to treat the incoming socket (or other file)
as text lines, use the "high[er] level" (and Standard!)
facilities of <stdio.h>.  On Un*x at least you can "wrap"
an already open socket in a stdio stream with fdopen().
Then use fgets(), which still needs to be passed
the buffer size explicitly, but stops at "newline"
(canonically one character in C) and appends
the null terminator to ensure a valid C string.

>  The equivalent in Ada is ...

--
- David.Thompson 1 now at worldnet.att.net












^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2000-08-21  1:44 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-13  0:00 Get_Line vs Adasockets Ultor
2000-08-13  0:00 ` Dale Stanbrough
2000-08-14  0:00   ` Martin Dowie
2000-08-17  0:00     ` tmoran
2000-08-21  0:00       ` Martin Dowie
2000-08-14  0:00 ` Ken Garlington
2000-08-14  0:00   ` Ultor
2000-08-14  0:00     ` Ted Dennison
2000-08-14  0:00       ` Michal Zalewski
2000-08-14  0:00         ` Ted Dennison
2000-08-21  1:44   ` David Thompson
2000-08-14  0:00 ` Anders Gidenstam

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