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:

 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

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 ""
port = port or 8070
if arg then
   host = arg[1] or host
   port = arg[2] or port
print("Binding to host '" "' and port " ..port.. "...")
server, err = socket.bind(host, port)
assert(server, err)
ip, port = server:getsockname()
assert(ip, port)
print("Waiting packets on " .. ip .. ":" .. port .. "...")

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

	if csess then

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

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


STOP MORE SPAM with the new MSN 8 and get 2 months FREE*