Unix Programming - How to listen on two sockets in the same application

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2006 > How to listen on two sockets in the same application





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to listen on two sockets in the same application
af300wsm@gmail.com

2006-12-15, 1:20 pm

Hello,

I'm writing a server program that will listen on both IPv4 and IPv6.
Thus, I'm creating two sockets for the two different address families
and will have to listen on both. However, in my past experience on
writing code using listen, the listen call blocks the application until
a connection request is made by some outside entity. Since I've got to
listen on two sockets, how do I accomplish this?

I'm thinking that something like this won't work:

listen( sock1, 5 ); // app is blocked here until connect from client
listen( sock2, 5 );

Is this going to be accomplished by somehow using fcntl to configure
the sockets for non-blocking and then using select on the sockets?

Oh, I don't know if this will add much complexity to things, but I'm
writing all this in C++ and am hoping to wrap the handling of
communication over the two respective address families within
classes/objects. Please bear that in mind with any answers.

Thanks everyone,

Andy

Pascal Bourguignon

2006-12-15, 1:20 pm

af300wsm@gmail.com writes:

> Hello,
>
> I'm writing a server program that will listen on both IPv4 and IPv6.
> Thus, I'm creating two sockets for the two different address families
> and will have to listen on both. However, in my past experience on
> writing code using listen, the listen call blocks the application until
> a connection request is made by some outside entity. Since I've got to
> listen on two sockets, how do I accomplish this?
>
> I'm thinking that something like this won't work:
>
> listen( sock1, 5 ); // app is blocked here until connect from client
> listen( sock2, 5 );


No, listen(2) doesn't block. It's accept(2) that may block. But you
can use select(2) or poll(2) when it exists on the socket before
calling accept.

--
__Pascal Bourguignon__ http://www.informatimago.com/

HEALTH WARNING: Care should be taken when lifting this product,
since its mass, and thus its weight, is dependent on its velocity
relative to the user.
Rainer Weikusat

2006-12-15, 1:20 pm

af300wsm@gmail.com writes:
> I'm writing a server program that will listen on both IPv4 and IPv6.
> Thus, I'm creating two sockets for the two different address families
> and will have to listen on both. However, in my past experience on
> writing code using listen, the listen call blocks the application until
> a connection request is made by some outside entity.


This is wrong. The listen-call just informs the kernel that the
application is willing to accept incoming connections on the
particular socket in question. What usually blocks is accept(2), until
such a connection is available.

> Since I've got to
> listen on two sockets, how do I accomplish this?
>
> I'm thinking that something like this won't work:
>
> listen( sock1, 5 ); // app is blocked here until connect from client
> listen( sock2, 5 );
>
> Is this going to be accomplished by somehow using fcntl to configure
> the sockets for non-blocking and then using select on the sockets?


If you want to do yourself and your machine a favor, do not use select
for small, fixed-size descriptor sets. Use poll.

Generally: You need to configure both sockets for non-blocking I/O
(setting O_NONBLOCK via fcntl F_GETFL) and then use 'some kind of
synchronous I/O multiplexing call' to wait for any of them to be ready
for input. You can then call accept on it and if that call doesn't
return an error (EAGAIN), you have a descriptor that refers to a
connection initiated by some client.
Maxim Yegorushkin

2006-12-15, 7:25 pm


Rainer Weikusat wrote:

> If you want to do yourself and your machine a favor, do not use select
> for small, fixed-size descriptor sets. Use poll.


Could you please elaborate how poll is better than select?

Brian C

2006-12-16, 1:31 am

Rainer Weikusat wrote:

> If you want to do yourself and your machine a favor, do not use select
> for small, fixed-size descriptor sets. Use poll.


Please enlighten me how this is so.
af300wsm@gmail.com

2006-12-18, 1:20 pm


Pascal Bourguignon wrote:
> af300wsm@gmail.com writes:
>
>
> No, listen(2) doesn't block. It's accept(2) that may block. But you
> can use select(2) or poll(2) when it exists on the socket before
> calling accept.


Yes, of course you're correct. I was mistaken in my memory. It is the
accept call that blocked that code I wrote several years ago.

So, since accept is the call that blocks, how would go about accepting
on the two sockets? Would that be accomplished by use of select or
poll (there seems to be differeing oppinions as to which is the
better)?

Andy

Rainer Weikusat

2006-12-18, 1:20 pm

"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> writes:
> Rainer Weikusat wrote:
>
> Could you please elaborate how poll is better than select?


That follows easily from the documenation of each.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com