Lua socket problem

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua socket problem

Thijs Schreijer

This code;

 

local socket = require("socket")
local PORT = 30000

local server1, server2

print (socket._VERSION)
server1=socket.tcp()
assert(server1:setoption('reuseaddr', true))
assert(server1:bind("*", PORT))
assert(server1:listen())

server2=socket.tcp()
assert(server2:setoption('reuseaddr', true))
assert(server2:bind("*", PORT))
assert(server2:listen())

 

runs fine on Windows, yet on Linux (Debian 7, WM ware image) the second `bind` (for server2) fails with a "socket already in use" error. Despite the 'reuseaddr' socket option.

 

Anyone seen this and knows how to resolve it?

 

any help is appreciated.

Thijs

 

(testing a server, which involves a lot of tests quickly stopping and starting the server. The abandonned sockets are sometimes not available again before the server restarts, which makes tests fail in an unpredictible manner)

Reply | Threaded
Open this post in threaded view
|

Re: Lua socket problem

Ignacio Burgueño-2

It is ok what you are trying to do (reusing the socket) but only when they are in time_wait. ie. your tests will run ok, but the code you posted won't.

In Windows, the get closed right away (so no need to set reuseaddr).
Reply | Threaded
Open this post in threaded view
|

Re: Lua socket problem

Oliver Kroth
In reply to this post by Thijs Schreijer
Tjis.

you will have to close the first socket before trying to reuse the address with the second socket.
The "reuseaddr" option tell sthe OS to allow to bind to the address immediately again. Without this, the address is blocked for the (estimated) time that any packet for the address is still strolling around in the internet.

--
Oliver

Am 04.12.2015 um 19:45 schrieb Thijs Schreijer:

This code;

 

local socket = require("socket")
local PORT = 30000

local server1, server2

print (socket._VERSION)
server1=socket.tcp()
assert(server1:setoption('reuseaddr', true))
assert(server1:bind("*", PORT))
assert(server1:listen())

server2=socket.tcp()
assert(server2:setoption('reuseaddr', true))
assert(server2:bind("*", PORT))
assert(server2:listen())

 

runs fine on Windows, yet on Linux (Debian 7, WM ware image) the second `bind` (for server2) fails with a "socket already in use" error. Despite the 'reuseaddr' socket option.

 

Anyone seen this and knows how to resolve it?

 

any help is appreciated.

Thijs

 

(testing a server, which involves a lot of tests quickly stopping and starting the server. The abandonned sockets are sometimes not available again before the server restarts, which makes tests fail in an unpredictible manner)