comp.lang.ada
 help / color / mirror / Atom feed
* [OT] Two sockets questions
@ 2003-05-07 18:03 Jano
  2003-05-07 18:22 ` Samuel Tardieu
  2003-05-07 20:09 ` tmoran
  0 siblings, 2 replies; 33+ messages in thread
From: Jano @ 2003-05-07 18:03 UTC (permalink / raw)


Well, this is at best mildly related to Gnat, but as this is the only 
programmers forum I frequent these times...

I'm experimenting with non-blocking IO in sockets (I'm using the 
Gnat.Sockets package), and I have two questions:

Is there a way of performing a connect without blocking on it? I suspect 
not... and if not in Ada, by other means?

Second: I have a connected socket which I poll periodically for data. 
When there is no more data available and the other endpoint has closed 
the socket, I continue receiving 0 byte available but not notification 
of closed socket. It's only when I try to read/write that a exception is 
raised about the connection reset or something. Is there a mean to be 
aware that connection has been closed (after the available data is 
exhausted, I presume)?

Thanks in advance,

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-07 18:03 [OT] Two sockets questions Jano
@ 2003-05-07 18:22 ` Samuel Tardieu
  2003-05-07 19:52   ` Stephen Leake
                     ` (2 more replies)
  2003-05-07 20:09 ` tmoran
  1 sibling, 3 replies; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-07 18:22 UTC (permalink / raw)


>>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:

Jano> Is there a way of performing a connect without blocking on it? I
Jano> suspect not... and if not in Ada, by other means?

Sure! I'll describe the process in C, I do not know whether
GNAT.Sockets allows you to do all those steps:

  1) Create your socket
  2) Make it non-blocking
  3) connect() will return immediately with either 0 (success), -1
     with errno set to EINPROGRESS or -1 with another error (real
     error).
  4) If the result was -1/EINPROGRESS, do a select() on the socket for
     writing. When it tells you that you can write, write() 0 bytes. If
     there is no error (write() returns 0), then you are
     connected. Otherwise, you will get an error (typically
     -1/ENOTCONN).

Jano> Second: I have a connected socket which I poll periodically for
Jano> data. When there is no more data available and the other
Jano> endpoint has closed the socket, I continue receiving 0 byte
Jano> available but not notification of closed socket. It's only when
Jano> I try to read/write that a exception is raised about the
Jano> connection reset or something. Is there a mean to be aware that
Jano> connection has been closed (after the available data is
Jano> exhausted, I presume)?

If you select() the socket for reading and select() tells you that
there is data available and if read() returns 0 bytes, then this means
that the other side has shutdown its sending part.

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-07 18:22 ` Samuel Tardieu
@ 2003-05-07 19:52   ` Stephen Leake
  2003-05-07 20:08     ` David C. Hoos
  2003-05-07 22:33     ` Jano
  2003-05-07 20:02   ` Jano
  2003-05-08 10:14   ` Preben Randhol
  2 siblings, 2 replies; 33+ messages in thread
From: Stephen Leake @ 2003-05-07 19:52 UTC (permalink / raw)
  Cc: sam

Samuel Tardieu <sam@rfc1149.net> writes:

> >>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:
> 
> Jano> Is there a way of performing a connect without blocking on it? I
> Jano> suspect not... and if not in Ada, by other means?
> 
> Sure! I'll describe the process in C, I do not know whether
> GNAT.Sockets allows you to do all those steps:
> 
>   1) Create your socket
>   2) Make it non-blocking
>   3) connect() will return immediately with either 0 (success), -1
>      with errno set to EINPROGRESS or -1 with another error (real
>      error).
>   4) If the result was -1/EINPROGRESS, do a select() on the socket for
>      writing. When it tells you that you can write, write() 0 bytes. If
>      there is no error (write() returns 0), then you are
>      connected. Otherwise, you will get an error (typically
>      -1/ENOTCONN).

This is not C. It is English. Hm, I suppose "EINPROGRESS" could be a C
macro, but it could equally well be an Ada named number.

I suspect Jano is wondering how to accomplish step 2; exactly what
function (C or Ada) should be called, with what parameters?

-- 
-- Stephe



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

* Re: [OT] Two sockets questions
  2003-05-07 18:22 ` Samuel Tardieu
  2003-05-07 19:52   ` Stephen Leake
@ 2003-05-07 20:02   ` Jano
  2003-05-08  8:57     ` Samuel Tardieu
  2003-05-08 10:14   ` Preben Randhol
  2 siblings, 1 reply; 33+ messages in thread
From: Jano @ 2003-05-07 20:02 UTC (permalink / raw)


Samuel Tardieu dice...

> Sure! I'll describe the process in C, I do not know whether
> GNAT.Sockets allows you to do all those steps:
> 
>   1) Create your socket
>   2) Make it non-blocking
>   3) connect() will return immediately with either 0 (success), -1
>      with errno set to EINPROGRESS or -1 with another error (real
>      error).
>   4) If the result was -1/EINPROGRESS, do a select() on the socket for
>      writing. When it tells you that you can write, write() 0 bytes. If
>      there is no error (write() returns 0), then you are
>      connected. Otherwise, you will get an error (typically
>      -1/ENOTCONN).

Great! I must try these things.

> Jano> Second: I have a connected socket which I poll periodically for
> Jano> data. When there is no more data available and the other
> Jano> endpoint has closed the socket, I continue receiving 0 byte
> Jano> available but not notification of closed socket. It's only when
> Jano> I try to read/write that a exception is raised about the
> Jano> connection reset or something. Is there a mean to be aware that
> Jano> connection has been closed (after the available data is
> Jano> exhausted, I presume)?
> 
> If you select() the socket for reading and select() tells you that
> there is data available and if read() returns 0 bytes, then this means
> that the other side has shutdown its sending part.

Mmm... The Gnat.Sockets package has a select binding... I wonder if it 
behaves that way. I will obviously test it ;-)

A idea that comes to me: If I do a read for 0 bytes, will occur the 
same? I mean, if connection is alive, nothing happens, and if it's dead, 
error...

Many thanks!

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-07 19:52   ` Stephen Leake
@ 2003-05-07 20:08     ` David C. Hoos
  2003-05-07 22:33     ` Jano
  1 sibling, 0 replies; 33+ messages in thread
From: David C. Hoos @ 2003-05-07 20:08 UTC (permalink / raw)



"Stephen Leake" <Stephe.Leake@nasa.gov> wrote in message
news:uk7d2lekh.fsf@nasa.gov...
> Samuel Tardieu <sam@rfc1149.net> writes:
>
> > >>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:
> >
> > Jano> Is there a way of performing a connect without blocking on it? I
> > Jano> suspect not... and if not in Ada, by other means?
> >
> > Sure! I'll describe the process in C, I do not know whether
> > GNAT.Sockets allows you to do all those steps:
> >
> >   1) Create your socket
> >   2) Make it non-blocking
> >   3) connect() will return immediately with either 0 (success), -1
> >      with errno set to EINPROGRESS or -1 with another error (real
> >      error).
> >   4) If the result was -1/EINPROGRESS, do a select() on the socket for
> >      writing. When it tells you that you can write, write() 0 bytes. If
> >      there is no error (write() returns 0), then you are
> >      connected. Otherwise, you will get an error (typically
> >      -1/ENOTCONN).
>
> This is not C. It is English. Hm, I suppose "EINPROGRESS" could be a C
> macro, but it could equally well be an Ada named number.
As Sam stated, EINPROGRESS is an errno value set by the connect()
function under some conditions.
>
> I suspect Jano is wondering how to accomplish step 2; exactly what
> function (C or Ada) should be called, with what parameters?
>
> -- 
> -- Stephe
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>





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

* Re: [OT] Two sockets questions
  2003-05-07 18:03 [OT] Two sockets questions Jano
  2003-05-07 18:22 ` Samuel Tardieu
@ 2003-05-07 20:09 ` tmoran
  1 sibling, 0 replies; 33+ messages in thread
From: tmoran @ 2003-05-07 20:09 UTC (permalink / raw)


> Is there a way of performing a connect without blocking on it? I suspect
> not... and if not in Ada, by other means?
Claw.Sockets.Non_Blocking does that under Windows.  It creates a hidden
window for the connect message, then dispatches to your When_Connect
(or When_No_Connect, if it times out) procedure.
  In fact, Claw.Sockets in the demo version (www.rrsoftware.com) uses
that mechanism internally, IIRC, for blocking sockets.  They are actually
opened as non-blocking (to allow for a time out), then made blocking
once connected.



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

* Re: [OT] Two sockets questions
  2003-05-07 19:52   ` Stephen Leake
  2003-05-07 20:08     ` David C. Hoos
@ 2003-05-07 22:33     ` Jano
  2003-05-08  9:10       ` Samuel Tardieu
  2003-05-08 17:48       ` Stephen Leake
  1 sibling, 2 replies; 33+ messages in thread
From: Jano @ 2003-05-07 22:33 UTC (permalink / raw)


Stephen Leake dice...

> >   1) Create your socket
> >   2) Make it non-blocking

> I suspect Jano is wondering how to accomplish step 2; exactly what
> function (C or Ada) should be called, with what parameters?

That's no problem. I have used it before in the Gnat binding. However, 
the Gnat thick binding raises a "Socket_error [10035] Operation would 
block" trying to connect so I guess I may need to go the thin way :( Oh 
well...

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-07 20:02   ` Jano
@ 2003-05-08  8:57     ` Samuel Tardieu
  2003-05-08 21:39       ` Jano
  0 siblings, 1 reply; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-08  8:57 UTC (permalink / raw)


>>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:

Jano> A idea that comes to me: If I do a read for 0 bytes, will occur
Jano> the same? I mean, if connection is alive, nothing happens, and
Jano> if it's dead, error...

I do not understand your question.

And a dead connection is not the same thing as a closed connection.

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-07 22:33     ` Jano
@ 2003-05-08  9:10       ` Samuel Tardieu
  2003-05-08 21:35         ` Jano
  2003-05-10 14:06         ` Jano
  2003-05-08 17:48       ` Stephen Leake
  1 sibling, 2 replies; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-08  9:10 UTC (permalink / raw)


>>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:

Jano> That's no problem. I have used it before in the Gnat
Jano> binding. However, the Gnat thick binding raises a "Socket_error
Jano> [10035] Operation would block" trying to connect

On what OS? The correct return code is undoubtly EINPROGRESS, but it
is possible that EINPROGRESS is equal to EWOULDBLOCK on some
platforms.

Jano> so I guess I may need to go the thin way :( Oh well...

Probably. It looks like GNAT.Sockets considers that -1 returned by
connect() is always an error and do not consider the asynchronous
case when connecting. You may be on your own for this one.

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-07 18:22 ` Samuel Tardieu
  2003-05-07 19:52   ` Stephen Leake
  2003-05-07 20:02   ` Jano
@ 2003-05-08 10:14   ` Preben Randhol
  2003-05-08 11:09     ` Samuel Tardieu
  2003-05-08 18:06     ` tmoran
  2 siblings, 2 replies; 33+ messages in thread
From: Preben Randhol @ 2003-05-08 10:14 UTC (permalink / raw)


Samuel Tardieu wrote:
>>>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:
> 
>Jano> Is there a way of performing a connect without blocking on it? I
>Jano> suspect not... and if not in Ada, by other means?
> 
> Sure! I'll describe the process in C, I do not know whether
> GNAT.Sockets allows you to do all those steps:
> 
>   1) Create your socket
>   2) Make it non-blocking
>   3) connect() will return immediately with either 0 (success), -1
>      with errno set to EINPROGRESS or -1 with another error (real
>      error).
>   4) If the result was -1/EINPROGRESS, do a select() on the socket for
>      writing. When it tells you that you can write, write() 0 bytes. If
>      there is no error (write() returns 0), then you are
>      connected. Otherwise, you will get an error (typically
>      -1/ENOTCONN).

Can Adasockets do this?


-- 
Preben Randhol                    http://www.pvv.org/~randhol/



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

* Re: [OT] Two sockets questions
  2003-05-08 10:14   ` Preben Randhol
@ 2003-05-08 11:09     ` Samuel Tardieu
  2003-05-08 21:40       ` Jano
  2003-05-08 18:06     ` tmoran
  1 sibling, 1 reply; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-08 11:09 UTC (permalink / raw)


>>>>> "Preben" == Preben Randhol <randhol+abuse@pvv.org> writes:

Preben> Can Adasockets do this?

Certainly if you use the thin binding (which is fully exposed). No
high-level library has been written for this specific case.

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-07 22:33     ` Jano
  2003-05-08  9:10       ` Samuel Tardieu
@ 2003-05-08 17:48       ` Stephen Leake
  2003-05-08 18:42         ` Samuel Tardieu
  2003-05-08 21:40         ` Jano
  1 sibling, 2 replies; 33+ messages in thread
From: Stephen Leake @ 2003-05-08 17:48 UTC (permalink / raw)


Jano <nono@celes.unizar.es> writes:

> Stephen Leake dice...
> 
> > >   1) Create your socket
> > >   2) Make it non-blocking
> 
> > I suspect Jano is wondering how to accomplish step 2; exactly what
> > function (C or Ada) should be called, with what parameters?
> 
> That's no problem. I have used it before in the Gnat binding. 

Um, what, exactly, is "it"? That is the real question here!

> However, the Gnat thick binding raises a "Socket_error [10035]
> Operation would block" trying to connect so I guess I may need to go
> the thin way :( Oh well...
> 
> -- 
> -------------------------
> Jano
> 402450.at.cepsz.unizar.es
> -------------------------

-- 
-- Stephe



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

* Re: [OT] Two sockets questions
  2003-05-08 10:14   ` Preben Randhol
  2003-05-08 11:09     ` Samuel Tardieu
@ 2003-05-08 18:06     ` tmoran
  1 sibling, 0 replies; 33+ messages in thread
From: tmoran @ 2003-05-08 18:06 UTC (permalink / raw)


re thick Ada sockets packages:  I recently sent one off to adapower, where
it should be posted soon.  It's a Claw-less version of Claw.Sockets (but
specifically does not contain Claw.Sockets.Non_Blocking, since that uses
Windows specific stuff).



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

* Re: [OT] Two sockets questions
  2003-05-08 17:48       ` Stephen Leake
@ 2003-05-08 18:42         ` Samuel Tardieu
  2003-05-08 21:41           ` Jano
  2003-05-08 21:48           ` Jano
  2003-05-08 21:40         ` Jano
  1 sibling, 2 replies; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-08 18:42 UTC (permalink / raw)


>>>>> "Stephen" == Stephen Leake <Stephe.Leake@nasa.gov> writes:

Stephen> Jano <nono@celes.unizar.es> writes:
>> Stephen Leake dice...
>>
>> > > 1) Create your socket
>> > > 2) Make it non-blocking
>>
>> > I suspect Jano is wondering how to accomplish step 2; exactly
>> > what function (C or Ada) should be called, with what parameters?
>>
>> That's no problem. I have used it before in the Gnat binding.

Stephen> Um, what, exactly, is "it"? That is the real question here!

Jano means that he knows perfectly well how to perform steps 1 and 2
(create a non-blocking socket). Your suspicion was wrong :)

The problem does not lie in creating a non-blocking socket, but only
in using connect() in a non-blocking way.

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-08  9:10       ` Samuel Tardieu
@ 2003-05-08 21:35         ` Jano
  2003-05-09  8:14           ` Samuel Tardieu
  2003-05-10 14:06         ` Jano
  1 sibling, 1 reply; 33+ messages in thread
From: Jano @ 2003-05-08 21:35 UTC (permalink / raw)


Samuel Tardieu dice...
> >>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:
> 
> Jano> That's no problem. I have used it before in the Gnat
> Jano> binding. However, the Gnat thick binding raises a "Socket_error
> Jano> [10035] Operation would block" trying to connect
> 
> On what OS? The correct return code is undoubtly EINPROGRESS, but it
> is possible that EINPROGRESS is equal to EWOULDBLOCK on some
> platforms.

XP here (3.15p)

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-08  8:57     ` Samuel Tardieu
@ 2003-05-08 21:39       ` Jano
  0 siblings, 0 replies; 33+ messages in thread
From: Jano @ 2003-05-08 21:39 UTC (permalink / raw)


Samuel Tardieu dice...
> >>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:
> 
> Jano> A idea that comes to me: If I do a read for 0 bytes, will occur
> Jano> the same? I mean, if connection is alive, nothing happens, and
> Jano> if it's dead, error...
> 
> I do not understand your question.

I mean: I try to read a 0 length vector. If the connection is okay, 
nothing happens. If the other end has shutdown and there is no remaining 
data to be read, some error is returned (even trying to read 0 bytes).

I see in the Gnat.Sockets comments that a read in a closed socket sets 
Last to -1. However, that option is not valid: If you try to read a 0-
length array from an alive connection, "Operation would block" is raised 
anyway.

(Now I wonder if doing so in a non-blocking socket would have some 
effect. I suppose no, and without blocking or exceptions. Kinda 
amusing).

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-08 11:09     ` Samuel Tardieu
@ 2003-05-08 21:40       ` Jano
  2003-05-09  8:19         ` Samuel Tardieu
  0 siblings, 1 reply; 33+ messages in thread
From: Jano @ 2003-05-08 21:40 UTC (permalink / raw)


Samuel Tardieu dice...
> >>>>> "Preben" == Preben Randhol <randhol+abuse@pvv.org> writes:
> 
> Preben> Can Adasockets do this?
> 
> Certainly if you use the thin binding (which is fully exposed). No
> high-level library has been written for this specific case.

Is the thin binding of adasockets portable? I remember I opted for Gnat 
sockets because I saw the non-blocking options. I missed them in 
adasockets. Maybe I looked wrongly (or not in the thin part. I didn't 
want really to mess with low level things).

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-08 17:48       ` Stephen Leake
  2003-05-08 18:42         ` Samuel Tardieu
@ 2003-05-08 21:40         ` Jano
  1 sibling, 0 replies; 33+ messages in thread
From: Jano @ 2003-05-08 21:40 UTC (permalink / raw)


Stephen Leake dice...
> Jano <nono@celes.unizar.es> writes:
> 
> > Stephen Leake dice...
> > 
> > > >   1) Create your socket
> > > >   2) Make it non-blocking
> > 
> > > I suspect Jano is wondering how to accomplish step 2; exactly what
> > > function (C or Ada) should be called, with what parameters?
> > 
> > That's no problem. I have used it before in the Gnat binding. 
> 
> Um, what, exactly, is "it"? That is the real question here!

Used it = the function to accomplish 2). Making a socket non-blocking 
with Gnat.Sockets.

I must improve my English skills. The effort of writing it makes me 
often to be ambiguous.

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-08 18:42         ` Samuel Tardieu
@ 2003-05-08 21:41           ` Jano
  2003-05-08 21:48           ` Jano
  1 sibling, 0 replies; 33+ messages in thread
From: Jano @ 2003-05-08 21:41 UTC (permalink / raw)


Samuel Tardieu dice...
> >>>>> "Stephen" == Stephen Leake <Stephe.Leake@nasa.gov> writes:
> 
> Stephen> Jano <nono@celes.unizar.es> writes:
> >> Stephen Leake dice...
> >>
> >> > > 1) Create your socket
> >> > > 2) Make it non-blocking
> >>
> >> > I suspect Jano is wondering how to accomplish step 2; exactly
> >> > what function (C or Ada) should be called, with what parameters?
> >>
> >> That's no problem. I have used it before in the Gnat binding.
> 
> Stephen> Um, what, exactly, is "it"? That is the real question here!
> 
> Jano means that he knows perfectly well how to perform steps 1 and 2
> (create a non-blocking socket). Your suspicion was wrong :)
> 
> The problem does not lie in creating a non-blocking socket, but only
> in using connect() in a non-blocking way.

Exactly :)

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-08 18:42         ` Samuel Tardieu
  2003-05-08 21:41           ` Jano
@ 2003-05-08 21:48           ` Jano
  2003-05-08 22:35             ` tmoran
  1 sibling, 1 reply; 33+ messages in thread
From: Jano @ 2003-05-08 21:48 UTC (permalink / raw)


In a side note, trying to do the "select" trick proposed yesterday to 
see if the connection is alive, I've found that the Gnat implementation 
has a side effect. It clearly specifies that a special descriptor is 
opened with each select_type to allow the abortion of a select. However, 
when you closes the select_type, that descriptor is in TIME_WAIT status 
(normal, I suppose).

As my "bright" idea was to create a select_type, select on the supplied 
socket to be tested with immediate time out, and close the selector 
afterwards, that implies that for every test you get a TIME_WAIT. Doing 
the test quite frequently (I was in my test doing it once a second or 
more!) leaves you quickly with a loooong list of descriptors in 
TIME_WAIT status.

FWIW.

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-08 21:48           ` Jano
@ 2003-05-08 22:35             ` tmoran
  2003-05-09 14:42               ` Jano
  0 siblings, 1 reply; 33+ messages in thread
From: tmoran @ 2003-05-08 22:35 UTC (permalink / raw)


I'm curious why you want to use non-blocking sockets.  It's generally
easier in Ada to use multiple tasks and let individual tasks block.



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

* Re: [OT] Two sockets questions
  2003-05-08 21:35         ` Jano
@ 2003-05-09  8:14           ` Samuel Tardieu
  2003-05-09 14:32             ` Jano
  0 siblings, 1 reply; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-09  8:14 UTC (permalink / raw)


>>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:

>> On what OS? The correct return code is undoubtly EINPROGRESS, but
>> it is possible that EINPROGRESS is equal to EWOULDBLOCK on some
>> platforms.

Jano> XP here (3.15p)

Sorry, can't help on platform-dependent issues then (I don't have any
Microsoft Windows around).

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-08 21:40       ` Jano
@ 2003-05-09  8:19         ` Samuel Tardieu
  0 siblings, 0 replies; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-09  8:19 UTC (permalink / raw)


>>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:

Jano> Is the thin binding of adasockets portable? I remember I opted
Jano> for Gnat sockets because I saw the non-blocking options. I
Jano> missed them in adasockets. Maybe I looked wrongly (or not in the
Jano> thin part. I didn't want really to mess with low level things).

It is portable in the sense it hasn't change a lot in the last few
years. But I offer no guarantee, as this is free software and I have
no paying customer with a contract saying that I won't change it :)

But if you want to stay with GNAT.Sockets, you can either request
enhancements (if you are an ACT customer), enhance it yourself and
propose your modifications or create a child package with specific
non-blocking operations.

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-09  8:14           ` Samuel Tardieu
@ 2003-05-09 14:32             ` Jano
  0 siblings, 0 replies; 33+ messages in thread
From: Jano @ 2003-05-09 14:32 UTC (permalink / raw)


Samuel Tardieu dice...
> >>>>> "Jano" == Jano  <nono@celes.unizar.es> writes:
> 
> >> On what OS? The correct return code is undoubtly EINPROGRESS, but
> >> it is possible that EINPROGRESS is equal to EWOULDBLOCK on some
> >> platforms.
> 
> Jano> XP here (3.15p)
> 
> Sorry, can't help on platform-dependent issues then (I don't have any
> Microsoft Windows around).

Nonetheless you've helped me already with these things I didn't knew :)

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-08 22:35             ` tmoran
@ 2003-05-09 14:42               ` Jano
  2003-05-10  8:57                 ` Tarjei T. Jensen
  2003-05-11  2:05                 ` tmoran
  0 siblings, 2 replies; 33+ messages in thread
From: Jano @ 2003-05-09 14:42 UTC (permalink / raw)


tmoran@acm.org dice...
> I'm curious why you want to use non-blocking sockets.  It's generally
> easier in Ada to use multiple tasks and let individual tasks block.

Leaving curiosity aside :)? Actually, blocking tasks is my preferred 
option (the one I knew previously and thus find easier). However, I'm 
implementing a p2p application (summary at http://agio.sourceforge.net) 
which likely will need maintaining hundreds or thousands of open 
connections. Also, checking for alive hosts quickly is very interesting 
(hence the connect question). Moreover, it's probable these hosts that I 
must check come in bursts.

I'm a bit afraid of using so many (say two thousands) tasks (are they 
really that light? What computer is necessary for that number? I have a 
pretty decent one, but...) but really I haven't past experiences about 
so many threads. However, as I've never seen an application so task 
intensive, I've think that that is for some reason (my httpd apache uses 
253 and is the most I've seen).

Other ideas I've considered is using selectors and group sockets in 
tasks (on how many sockets can wait a selector?) and, finally, use non-
blocking sockets and use the lowest possible number of tasks.

However, for connect, I can't use select.

P.s: About how many bandwidth consumes a TCP connection for keep-alive? 
:) bonus question I've just think.

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



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

* Re: [OT] Two sockets questions
  2003-05-09 14:42               ` Jano
@ 2003-05-10  8:57                 ` Tarjei T. Jensen
  2003-05-10 16:55                   ` Simon Wright
  2003-05-11 18:23                   ` Tarjei T. Jensen
  2003-05-11  2:05                 ` tmoran
  1 sibling, 2 replies; 33+ messages in thread
From: Tarjei T. Jensen @ 2003-05-10  8:57 UTC (permalink / raw)



"Jano"  wrote
> I'm a bit afraid of using so many (say two thousands) tasks (are they
> really that light? What computer is necessary for that number? I have a
> pretty decent one, but...) but really I haven't past experiences about
> so many threads. However, as I've never seen an application so task
> intensive, I've think that that is for some reason (my httpd apache uses
> 253 and is the most I've seen).

You should read up on recent best practices for socket programming. I think
there has been some performance improvements due to better programming.

I don't remember what is supposed to be the best way these days or I would
have mentioned it.

You might get a clue from reading about performance diffences between
windows and Linux.

I notice that the squid developers report that they have reduced CPU usage
by using real time calls.

greetings,




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

* Re: [OT] Two sockets questions
  2003-05-08  9:10       ` Samuel Tardieu
  2003-05-08 21:35         ` Jano
@ 2003-05-10 14:06         ` Jano
  2003-05-10 18:01           ` tmoran
  1 sibling, 1 reply; 33+ messages in thread
From: Jano @ 2003-05-10 14:06 UTC (permalink / raw)


Samuel Tardieu <sam@rfc1149.net> wrote in message news:<87wuh1g5wy.fsf@inf.enst.fr>...
 
> On what OS? The correct return code is undoubtly EINPROGRESS, but it
> is possible that EINPROGRESS is equal to EWOULDBLOCK on some
> platforms.

This has been found to be what's happening. Michael Bode has pointed
in the Gnat list the article where Microsoft explains that Windows
will return EWOULDBLOCK in an asynchronous connect. The connect is in
effect being performed and Gnat is simply reporting the underlying
Windows error.

Case solved. Many thanks to all who have apported valuable knowledge
and interest.

Jano.

P.s: The article is (beware of line split)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/connect_2.asp



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

* Re: [OT] Two sockets questions
  2003-05-10  8:57                 ` Tarjei T. Jensen
@ 2003-05-10 16:55                   ` Simon Wright
  2003-05-11 18:23                   ` Tarjei T. Jensen
  1 sibling, 0 replies; 33+ messages in thread
From: Simon Wright @ 2003-05-10 16:55 UTC (permalink / raw)


"Tarjei T. Jensen" <tarjei.jensen@akerkvaerner.com> writes:

> I notice that the squid developers report that they have reduced CPU
> usage by using real time calls.

A colleague tells me his project had a lot of trouble working with
Linux real-time signals and sockets. RH 8.2, I think. They moved to
using plain select() (this part of the project is in C) and are now
happy. They don't have that many clients, though.



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

* Re: [OT] Two sockets questions
  2003-05-10 14:06         ` Jano
@ 2003-05-10 18:01           ` tmoran
  2003-05-10 21:27             ` Samuel Tardieu
  0 siblings, 1 reply; 33+ messages in thread
From: tmoran @ 2003-05-10 18:01 UTC (permalink / raw)


>in the Gnat list the article where Microsoft explains that Windows
>will return EWOULDBLOCK in an asynchronous connect. The connect is in
>effect being performed and Gnat is simply reporting the underlying
>Windows error.
  The Open code in the Claw-less sockets that will soon(?) appear on
www.adapower.com contains:

    if Connect(Socket.Handle, Sockaddr'unchecked_access) = Socket_Error then
      Error := WSAGetLastError;
      if Error = WSAE_Wouldblock then
        loop
          FD.Count := 1;
          FD.Set(FD.Set'first) := Socket.Handle;
          Result
            := Select_function(0,
                               Read_FD_Set       => Null_FD_Set'unchecked_access,
                               Write_FD_Set      => FD'unchecked_access,
                               Exceptions_FD_Set => Null_FD_Set'unchecked_access,
                               Timeout           => Zerowait'unchecked_access);
          exit when Result = 1;
          if Result = Socket_Error then
          ...
It's intended for blocking sockets, so it sits around and polls till the
select says its writable, or some other error is returned.  It actually
opens as non-blocking so it can get control back from the OS to do this
polling, and timeout checking, etc.



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

* Re: [OT] Two sockets questions
  2003-05-10 18:01           ` tmoran
@ 2003-05-10 21:27             ` Samuel Tardieu
  2003-05-11 18:01               ` tmoran
  0 siblings, 1 reply; 33+ messages in thread
From: Samuel Tardieu @ 2003-05-10 21:27 UTC (permalink / raw)


>>>>> "Tom" == Tom Moran <tmoran@acm.org> writes:

Tom> It's intended for blocking sockets, so it sits around and polls
Tom> till the select says its writable, or some other error is
Tom> returned.  It actually opens as non-blocking so it can get
Tom> control back from the OS to do this polling, and timeout
Tom> checking, etc.

Why not make the socket blocking in this case (around connect()) since
your code sits and waits for select() to return?

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/sam



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

* Re: [OT] Two sockets questions
  2003-05-09 14:42               ` Jano
  2003-05-10  8:57                 ` Tarjei T. Jensen
@ 2003-05-11  2:05                 ` tmoran
  1 sibling, 0 replies; 33+ messages in thread
From: tmoran @ 2003-05-11  2:05 UTC (permalink / raw)


>I'm a bit afraid of using so many (say two thousands) tasks (are they
>really that light? What computer is necessary for that number? I have a
  Out of curiosity, I just successfully ran an, admittedly trivial,
program with 2,000 simultaneous tasks.
  IIRC BTW, Windows sockets are polled, not interrupting.  Non-blocking
sockets send messages to the desired window, and you watch for them
by polling PeekMessage.



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

* Re: [OT] Two sockets questions
  2003-05-10 21:27             ` Samuel Tardieu
@ 2003-05-11 18:01               ` tmoran
  0 siblings, 0 replies; 33+ messages in thread
From: tmoran @ 2003-05-11 18:01 UTC (permalink / raw)


> Tom> It's intended for blocking sockets, so it sits around and polls
> Tom> till the select says its writable, or some other error is
> Tom> returned.  It actually opens as non-blocking so it can get
> Tom> control back from the OS to do this polling, and timeout
> Tom> checking, etc.
>
> Why not make the socket blocking in this case (around connect()) since
> your code sits and waits for select() to return?
  Three reasons:  The Open procedure has a timeout parameter, so it needs
to be able to terminate the "connect" early.  The package also has a
"Request_Quit" mechanism whereby some task can ask another to abort a wait
on socket activity.  It's simpler and clearer to check with a direct poll
loop (with "delay") than with Windows' message loop polling.  Also, one of
the targeted compilers does its own tasking within a single Windows
thread, so a true block of the thread would block all tasks.  (That
compiler's Ada task switch time, not going through the OS, is quite a bit
faster.  It also can target Windows 3.1, though I grant that may no longer
be very important.)



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

* Re: [OT] Two sockets questions
  2003-05-10  8:57                 ` Tarjei T. Jensen
  2003-05-10 16:55                   ` Simon Wright
@ 2003-05-11 18:23                   ` Tarjei T. Jensen
  1 sibling, 0 replies; 33+ messages in thread
From: Tarjei T. Jensen @ 2003-05-11 18:23 UTC (permalink / raw)



"Tarjei T. Jensen" <tarjei.jensen@akerkvaerner.com> wrote in message
news:3ebcbefb$1@news.wineasy.se...
>
> "Jano"  wrote
> > I'm a bit afraid of using so many (say two thousands) tasks (are they
> > really that light? What computer is necessary for that number? I have a
> > pretty decent one, but...) but really I haven't past experiences about
> > so many threads. However, as I've never seen an application so task
> > intensive, I've think that that is for some reason (my httpd apache uses
> > 253 and is the most I've seen).

New answer (google is my friend)

The Winsock programmers FAQ http://tangentsoft.net/wskfaq/ says
2.8 - Winsock keeps returning the error WSAEWOULDBLOCK. What's wrong with my
program?
Not a thing. WSAEWOULDBLOCK is a perfectly normal occurrence in programs
using non-blocking and asynchronous sockets. It's Winsock's way of telling
your program "I can't do that right now, because I would have to block to do
so."

The next question is, how do you know when it's safe to try again? In the
case of asynchronous sockets, Winsock will send you an FD_WRITE message
after a failed send() call when it is safe to write; it will send you an
FD_READ message after a recv() call when more data arrives on that socket.
Similarly, in a non-blocking sockets program that uses select(), the
writefds will be set when it's okay to write, and the readfds will be set if
there is data to read.

Note that Win9x has a bug where select() can fail to block on a nonblocking
socket. It will signal one of the sockets, which will cause your program to
call recv() or send() or similar. That function will return WSAEWOULDBLOCK,
which can be quite a surprise. So, a program using select() under Win9x has
to be able to deal with this error at any time.

This gets to a larger issue: whenever you use some form of nonblocking
sockets, you have to be prepared for WSAEWOULDBLOCK at any time. It's simply
a matter of defensive programming, just like checking for null pointers.



Quote from the Unix socket FAQ (http://www.developerweb.net/sock-faq/):
9. What are the pros/cons of select(), non-blocking I/O and SIGIO?

Using non-blocking I/O means that you have to poll sockets to see if there
is data to be read from them. Polling should usually be avoided since it
uses more CPU time than other techniques.

Using SIGIO allows your application to do what it does and have the
operating system tell it (with a signal) that there is data waiting for it
on a socket. The only drawback to this soltion is that it can be confusing,
and if you are dealing with multiple sockets you will have to do a select()
anyway to find out which one(s) is ready to be read.

Using select() is great if your application has to accept data from more
than one socket at a time since it will block until any one of a number of
sockets is ready with data. One other advantage to select() is that you can
set a time-out value after which control will be returned to you whether any
of the sockets have data for you or not.


-- End of Quote --

Related Web sites:

Winsock tuning FAQ http://www.cerberus-sys.com/~belleisl/mtu_mss_rwin.html

Raw IP Networking FAQ http://www.whitefang.com/rin/

Unix programming FAQ http://www.erlenstar.demon.co.uk/unix/

The TCP/IP FAQ http://www.itprc.com/tcpipfaq/default.htm





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

end of thread, other threads:[~2003-05-11 18:23 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-07 18:03 [OT] Two sockets questions Jano
2003-05-07 18:22 ` Samuel Tardieu
2003-05-07 19:52   ` Stephen Leake
2003-05-07 20:08     ` David C. Hoos
2003-05-07 22:33     ` Jano
2003-05-08  9:10       ` Samuel Tardieu
2003-05-08 21:35         ` Jano
2003-05-09  8:14           ` Samuel Tardieu
2003-05-09 14:32             ` Jano
2003-05-10 14:06         ` Jano
2003-05-10 18:01           ` tmoran
2003-05-10 21:27             ` Samuel Tardieu
2003-05-11 18:01               ` tmoran
2003-05-08 17:48       ` Stephen Leake
2003-05-08 18:42         ` Samuel Tardieu
2003-05-08 21:41           ` Jano
2003-05-08 21:48           ` Jano
2003-05-08 22:35             ` tmoran
2003-05-09 14:42               ` Jano
2003-05-10  8:57                 ` Tarjei T. Jensen
2003-05-10 16:55                   ` Simon Wright
2003-05-11 18:23                   ` Tarjei T. Jensen
2003-05-11  2:05                 ` tmoran
2003-05-08 21:40         ` Jano
2003-05-07 20:02   ` Jano
2003-05-08  8:57     ` Samuel Tardieu
2003-05-08 21:39       ` Jano
2003-05-08 10:14   ` Preben Randhol
2003-05-08 11:09     ` Samuel Tardieu
2003-05-08 21:40       ` Jano
2003-05-09  8:19         ` Samuel Tardieu
2003-05-08 18:06     ` tmoran
2003-05-07 20:09 ` tmoran

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