comp.lang.ada
 help / color / mirror / Atom feed
From: "David Thompson" <david.thompson1@worldnet.att.net>
Subject: Re: Get_Line vs Adasockets
Date: Mon, 21 Aug 2000 01:44:01 GMT
Date: 2000-08-21T01:44:01+00:00	[thread overview]
Message-ID: <BP%n5.6536$%O6.395127@bgtnsc05-news.ops.worldnet.att.net> (raw)
In-Reply-To: ZXKl5.3050$ic.175523@news.flash.net

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












      parent reply	other threads:[~2000-08-21  1:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Anders Gidenstam
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 [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