Full duplex socket communication

  • Follow


I'm trying to write client/server networking applications and I've hit
a bit of a snag.  After establishing a connection between client and
server, data needs to flow asynchronously; that is, I want both the
client and server to be able to initiate data transfers to the other
node.

The select() function immediately comes to mind.  If I understand this
function correctly, it can block until there is data to read or it's
safe to write.  However, most of the time it will be safe to write and
there will be nothing for it to do. Then I'll need to call select()
again... and I'll have an ugly busy-waiting loop.  Someone please tell
me if I'm wrong...

The only other solution I can think of would be to create a
multi-threaded application, which would create an entirely new set of
problems I don't want to think about right now.  Thanks in advance...
0
Reply jahurt 7/8/2003 10:04:13 PM

: jahurt@hotmail.com
: I'm trying to write client/server networking applications and I've hit
: a bit of a snag.  After establishing a connection between client and
: server, data needs to flow asynchronously; that is, I want both the
: client and server to be able to initiate data transfers to the other
: node. 
: 
: The select() function immediately comes to mind.  If I understand this
: function correctly, it can block until there is data to read or it's
: safe to write.  However, most of the time it will be safe to write and
: there will be nothing for it to do.  Then I'll need to call select()
: again...  and I'll have an ugly busy-waiting loop.  Someone please
: tell me if I'm wrong... 

Do you have anything to write?  If not, why are you asking select
to wake you up when it's safe to write?

Hence: to avoid the busy loop, simply select only on the various inputs
if you have nothing to write: if you DO have something to write (and
need to ensure it's safe, ie, won't block) you could include the write
socket in the select; but then, you won't be in a busy loop, you'll
be writing something, then deciding what to wait on the next time.

Or put it this way: you select only on events for which you will have
work to do; if the only thing you would do for an event is wait some
more, you don't ask to wake up for that event, since you're already waiting. 


Wayne Throop   throopw@sheol.org   http://sheol.org/throopw
0
Reply throopw 7/9/2003 12:20:42 AM


jahurt@hotmail.com wrote:
> 
> Tobias Oed <tobias@physics.odu.edu> wrote in message news:<beffnu$4i1k1$1@ID-97389.news.dfncis.de>...
> > jahurt@hotmail.com wrote:
> >
> > > I'm trying to write client/server networking applications and I've hit
> > > a bit of a snag.  After establishing a connection between client and
> > > server, data needs to flow asynchronously; that is, I want both the
> > > client and server to be able to initiate data transfers to the other
> > > node.
> > >
> > > The select() function immediately comes to mind.  If I understand this
> > > function correctly, it can block until there is data to read or it's
> > > safe to write.  However, most of the time it will be safe to write and
> > > there will be nothing for it to do. Then I'll need to call select()
> > > again... and I'll have an ugly busy-waiting loop.  Someone please tell
> > > me if I'm wrong...
> >
> > If you don't have anything to write don't include that file descriptor in
> > the set you pass to select.
> > Tobias
> 
> Thanks for the reply... perhaps I wasn't clear in my description of
> the problem.
> Data needs to flow ansynchronously - that is, data could become
> available at any time for writing.  If select() is waiting for a
> socket to become available for reading, then I can't write...

    ... but if select() is waiting for something, just how
does data "become available" for writing?  Your program is
sitting there, stuck in select() and doing nothing at all;
in particular, it's not generating any data to be written.

    If your program is multi-threaded, with T1 stuck in select()
while T2 generates data for output, nothing's preventing T2
from writing its data whenever it pleases.

    Summary: I still don't understand what's troubling you.

-- 
Eric.Sosman@sun.com
0
Reply Eric 7/9/2003 6:17:03 PM

<jahurt@hotmail.com> wrote in message
news:d4ef389d.0307090916.58ecc3bd@posting.google.com...

> Thanks for the reply... perhaps I wasn't clear in my description of
> the problem.

    Definitely.

> Data needs to flow ansynchronously - that is, data could become
> available at any time for writing.

    How would data become available for writing exactly?

> If select() is waiting for a
> socket to become available for reading, then I can't write...

    Yes, but if 'select' is waiting  for a socket to become available for
reading, you can't do anything else at all. So where would this data that
became available for writing become available *from*?

    I'm presuming we're not talking about multithreading. If you are, then
the thread that generates the data that needs to be written can either write
it itself or force the thread that's in 'select' to stop selecting (say by
sending a byte on a pipe it's also selecting on).

    DS


0
Reply David 7/10/2003 3:19:58 AM

3 Replies
170 Views

(page loaded in 0.076 seconds)

5/23/2013 4:08:00 AM


Reply: