lua-users home
lua-l archive

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



Hello everyone!

New LUA user here so please bear with me =)
I have a problem with luasocket 2.0 that I cant solve. Have tried searching the archive but havent found anything useful yet. Im trying to start a TCP socket in nonblocking mode using settimeout(1) and then accept(). According to Luasocket2.0 reference:

 server:accept()
 ===============
 ...
 If a connection is successfully initiated, a client
 object is returned. If a timeout condition is met,
 the method returns nil followed by the error string
 'timeout'.
 ...

So after accept() returns I check the first return value. If its not nil I have a client connected, right? Well it seems to always return a non nil value even though its got no incoming client!? Also tried checking the second argument for "timeout" string but this also failed. This is driving me nuts and Im positive its something simple that I have just missed. Any suggestions are appreciated =) Heres the code:




--
***************************************************************************
host = host or "127.0.0.1"
port = port or 8070
if arg then
   host = arg[1] or host
   port = arg[2] or port
end
print("Binding to host '" ..host.. "' and port " ..port.. "...")
server, err = socket.bind(host, port)
assert(server, err)
server:settimeout(1)
ip, port = server:getsockname()
assert(ip, port)
print("Waiting packets on " .. ip .. ":" .. port .. "...")

while 1 do
	print("Trying to accept conn " .. os.date())
	csess, status = server:accept()

	if csess then
		print("accept")
		break
	end
end

print("Client connected")
while 1 do
	data, err = csess:receive("*l")
	if err then
		print("Client closed connection")
		os.exit()
	end
	if data then
		print("Echoing " .. data .. " from " .. ip .. ":" .. port)
		csess:send(data)
	end
end
--
***************************************************************************


This script can be started without parameters.
Was gonna test client using "telnet localhost 8070".



/Bor

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail