lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2009/11/10 Jerome Vuarand <jerome.vuarand@gmail.com>:
> I'm trying to write a copas-like module that handles both incoming and
> outgoing connections. Since the socket:connect operation can take some
> time, and I may have other sockets ready to do something, I'm setting
> the timeout to zero, I call connect, but then select is not working as
> expected on Windows.
>
> Here is a test script:
>
> #!/usr/bin/env lua
>
> require 'socket'
>
> function test(ip, port)
>    local master = assert(socket.tcp())
>    master:settimeout(0)
>    print(master)
>    local success,err = master:connect(ip, port)
>    print(master, success, err)
>    if not success and err=='timeout' then
>        socket.select({}, {master})
>        success,err = master:connect(ip, port)
>        print(master, success,err)
>    end
> end
>
> print("valid address 1")
> test('127.0.0.1', 80)
> print("valid address 2")
> test('127.0.0.1', 2000)
> print("invalid address")
> test('127.0.0.1', 44444)
>
> Note that I have a server on port 80 (Apache), and a custom LuaSocket
> server on port 2000. There is nothing on port 44444.
>
> On Linux, select returns as soon as the connection status is known. I
> have to call connect again to determine if it succeeded or not, here
> is the log:
>
> doub@blimbox:~/test$ lua test
> valid address 1
> tcp{master}: 0x80738d4
> tcp{client}: 0x80738d4  nil     timeout
> tcp{client}: 0x80738d4  1       nil
> valid address 2
> tcp{master}: 0x8075b0c
> connection on PIC
> tcp{client}: 0x8075b0c  nil     timeout
> tcp{client}: 0x8075b0c  1       nil
> invalid address
> tcp{master}: 0x8077d4c
> tcp{client}: 0x8077d4c  nil     timeout
> tcp{client}: 0x8077d4c  nil     connection refused
> doub@blimbox:~/test$
>
> On Windows however, when the connection succeeds, the socket is
> already connected and calling connect again triggers an error. And if
> the connection fails, select never returns. Note the Ctrl+C in the log
> that I had to do to kill the program :
>
> C:\test>lua test
> valid address 1
> tcp{master}: 00465008
> tcp{client}: 00465008   nil     timeout
> tcp{client}: 00465008   nil     already connected
> valid address 2
> tcp{master}: 00467078
> tcp{client}: 00467078   nil     timeout
> tcp{client}: 00467078   nil     already connected
> invalid address
> tcp{master}: 00469150
> tcp{client}: 00469150   nil     timeout
> ^C
> C:\test>
>
> On both platforms I'm using LuaSocket 2.0.2.
>
> Can anybody else reproduce the problem ? Is there a known workaround ?
> I haven't tried the 2.0.3 beta, should I ? Any help would be
> appreciated.

A quick test on a friend's mac shows that on OSX the behaviour is
again different: on failure select returns, and connect returns
nil,"connection failed", while on success select also returns, but
connect returns nil,"already connected". Am I hitting some undefined
use case of the BSD sockets API ?