[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Using LuaSocket with non-blocking sockets
- From: Sam Roberts <vieuxtech@...>
- Date: Thu, 15 Oct 2009 10:21:53 -0700
On Sun, Oct 11, 2009 at 5:28 PM, Jonathan Castello <solugon@hotmail.com> wrote:
> I'm using non-blocking sockets with LuaSocket under Win32, and I find myself looking for a way to tell if a connect() call failed. I can use socket.select(nil, {sock}, 0) to see if the socket has connected successfully, but I don't see any way to tell if it failed. Looking over the WinSock documentation, I notice an 'exceptfds' parameter that comes after the read and write tables, but LuaSocket doesn't appear to use it at all (its source shows NULL used instead). The WinSock documentation says that if a nonblocking socket failed to connect, passing it in as part of 'exceptfds' would return the socket as part of that list.
I doubt that that winsocket works like that, it would be completely
incompatible with the BSD socket API.
You're not the first one to be confused by the documentaion, though,
"exception" in this case doesn't mean "error", it means "exceptional
data", which with TCP means "out-of-band data is pending on the
socket". OOB data isn't commonly used outside of older unix protocols
like rsh, and telnet.
See
http://www.cl.cam.ac.uk/cgi-bin/manpage?2+select_tut
Anyhow, if connect fails (or any other asynchronous error occurs on a
socket, such as a connected socket getting closed), select will return
it as either readable, writeable, or both (which you can see below,
I'm on a linux box).
Then, when you attempt to use the socket, the call will return an error.
Cheers,
Sam
~/w % lua -l socket
Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
> s=socket.tcp() s:settimeout(0) return s:connect("192.168.41.2", 9000)
nil timeout
> r,w, e = socket.select({s}, {s}, 0)
> return r[1]
tcp{client}: 0x655fd8
> return w[1]
tcp{client}: 0x655fd8
> return e
nil
> = s
tcp{client}: 0x655fd8
> = s:receive("*l")
nil connection refused
> return s:send("*l")
nil closed 0
> s=socket.tcp() s:settimeout(0) return s:connect("192.168.41.2", 9000)
nil timeout
> r,w, e = socket.select({s}, {s}, 0)
> return s:send("hi")
nil connection refused 0