[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: socket select() question
- From: Diego Nehab <diego@...>
- Date: Tue, 13 Dec 2005 01:14:24 -0500 (EST)
this question is supposed to be for Diego and the LuaSocket guys.
however, it's more on the C side rather than Lua. i'm apprehensive on
mailing Diego directly.
Hmmm. I didn't mean to sound scary in any of my previous posts. :)
select() accepts a two tables. one for those that can be wriiten to
without blocking and another that can be read fronm without blocking.
You shouldn't think about it like this. Unless you put your socket in
non-blocking mode, you will end up blocked. One day you will ask for one
extra byte and block, or try to write one extra byte and block. So the
idea is to place the sockets in non-blocking mode, use select to block
you until something interesting happens, and then try to read or write.
If it fails with timeout, you call select again to block until something
interesting happens. Then you just try again. Select allows you to avoid
a busy wait, got it?
if i read from a file descriptor and my buffer isn't big enough to
contain all that is read:
1.1) how will i know that there's still more to read from the file/socket?
This is a tough question. It depends on the protocol. Does the other
side send all it can and then close the connection? If so, you can check
for a "closed" return error (see how I check for that in the source
code). The other side might inform you how many bytes to read. In that
case you read as many bytes as you are told.
1.2) do i have to read again from the file descriptor or is it safe to
pass it back again to select?
It is safe to do things in whatever order you like.
from the Luasocket reference:
"Another important note: calling select with a server socket in the
receive parameter before a call to accept does not guarantee accept
will return immediately. Use the settimeout method or accept might
2.1) is this applicable to all platforms?
I believe so. The problem is that, between the time select tells you
there is a connection waiting and the time you actually calls accept,
the other side might have cancelled the connection attempt. Then you
might block forever on accept.