Re: Question on select() and sockets



In article
<232789a3-7349-4887-9e3a-cfdf358f9744@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
K-mart Cashier <cdalten@xxxxxxxxx> wrote:

On Jun 6, 2:07 pm, Alex Fraser <m...@xxxxxxxxxxx> wrote:
K-mart Cashier wrote:
Why do sockets have to be 'non-blocking' when using select() to set
the timeout? Ie, why can't they be blocked?

You can use blocking calls to send()/recv()/read()/write() if you don't
care if you block in them - but I don't think that should ever be the
case if you are using select().

Making the sockets non-blocking guarantees control over when you block
(ie, in select()). Although I was writing about pipes in my recent reply
to Edd, the same applies to sockets (first block of my reply):

http://groups.google.co.uk/group/comp.unix.programmer/msg/b589bcb9068...

Alex

Perhaps I'm not thinking this through, but how

"This all sounds fine. But you must set the pipes non-blocking else
you
can still end up blocking, even if you only call read() or write()
after
select() says the descriptor is, respectively, readable or writeable:


Is this possible?

Chad

Yes. Select() will report the descriptor as writable as soon as there's
any room in the pipe or socket buffer. If you try to write more than
this, and it's not set to non-blocking, you'll block.

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.



Relevant Pages

  • Re: Close a blocked socket
    ... ..NET, that non-blocking model is easier and should scale better, you decided ... by dedicating a thread or two to the socket i/o and using blocking ... using blocking sockets, then good for you. ... connection to the Internet on the server side. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Socket Send
    ... The sockets are blocking. ... Set the socket to non-blocking. ... I'm just curious as to what benefits reading a 4k block does? ...
    (comp.unix.programmer)
  • Re: help using sockets, and OOP?
    ... sockets can be either blocking or non-blocking. ... you'll quickly find that asynchronous programming becomes ...
    (comp.lang.python)
  • Re: blocking non blocking
    ... Ok now i am very confused...here is why i think non blocking is less ... non blocking sockets typically involve some busy waiting and ... somehow a server with 50K threads, ... or overlapped I/O servicing 50K clients. ...
    (microsoft.public.win32.programmer.networks)
  • Re: blocking non blocking
    ... not connected to incoming data on the same thread, async sockets have to be ... somehow a server with 50K threads, ... or overlapped I/O servicing 50K clients. ... read and learnt about blocking and non blocking, ...
    (microsoft.public.win32.programmer.networks)