[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Using luasocket to read from the keyboard
- From: Diego Nehab <diego@...>
- Date: Sun, 8 Jun 2008 05:02:34 -0400 (EDT)
Hi,
I didn't really expect it to work on Windows, as they tend
to fudge things around a bit regarding sockets/files,
however I had more hope under Linux.
You are being nice. Sockets under WinSock are not regular
files with a file descriptor. And select doesn't even work
on regular file descriptors. This is probably the one
LuaSocket program that would behave completely different on
Windows vs. rest. :)
nRead = read (s, buf, sizeof(buf) - 1)
As I said, LuaSocket uses recv(). On my Mac, recv() refuses
to read from a regular descriptor. I think it is safe for
you to change recv/send to read/write on your version of
LuaSocket and see if it helps you.
So basically it was the same, and it worked (I think)
regardless of how much you typed. Maybe the keyboard
buffered a line, but then you only got the "select"
notification when something was there.
Even if you didn't type ENTER?
On the other hand, stdin is probably set to blocking mode,
so when there is nothing available, it recv will block until
all the data you need shows up. Also, keyboard input will be
buffered until you hit return, I guess.
In my C program I don't think I turned blocking off at all.
POSIX says read() should block until there is enough data.
So if you ask for 11 bytes and there are only 10 there, you
will block until the next byte arrives. That is, unless you
set O_NONBLOCK. So you can never be sure...
If you really want to use keyboard:receive(), you have to
call setfd on a *connected* socket. The socket returned
by socket.tcp() does not support the receive method.
Ah, you mean make some sort of dummy connection, and then
switch FDs? How would that work?
Disgusting, eh? :) You can probably use the same server to
create that dummy connected socket. Then, if you change
recv() to read(), yes. It *should* work.
[]s,
Diego