As the title says... Chat on...

User avatar
By bax
#9512 Using LuaLoader, if I load this file, called daytime_X.lua with the following commands loadfile works as expected.
Code: Select alldaytime_X.lua
=============
print('daytime.lua started')
conn = nil
local dt
conn=net.createConnection(net.TCP, 0)
-- show the server returned payload
conn:on("receive", function(conn, payload)
         print('\nreceived')
                        print(payload)
                        print('extracted Date and Time')
         dt = '20'..string.sub(payload,8,14)..
               string.sub(payload,15,24)
         print("Date/Time: " .. dt)
         end)

-- show disconnection
conn:on("disconnection", function(conn, payload) print('\nDisconnected') return dt end)
--connect                                   
conn:connect(13,'utcnist2.colorado.edu')

function foo()
   return dt
end


Code: Select allLuaLoader commands:
==================

restart
> IP unavaiable, Waiting...
Config done, IP is 192.168.0.190
X = assert(loadfile('daytime_X.lua')) <-- paste text --> send
> X()                                 <-- paste text --> send
daytime.lua started
>
received

57064 15-02-11 20:30:35 00 0 0 505.5 UTC(NIST) *

extracted Date and Time
Date/Time: 2015-02-11 20:30:35

Disconnected
print(foo())                          <-- paste text --> send
2015-02-11 20:30:35 --> OK, as expected
>

Now, if make the same calls in this file, I get the following errors and a reboot,

dofile("TCPserver_X.lua")
> X <-- command sent from PC TCP client
Get Nist time
daytime.lua started
PANIC: unprotected error in call to Lua API (TCPserver_X.lua:13: attempt to concatenate upvalue 'dt' (a nil value))
PANIC: unprotected error in call to Lua API (attempt to call a string value)
c_ÇÏRSöâFjSöfJSúâêá

Code: Select allTCPserver_X.lua
===============
-- zeroday
-- A simple http server (changed to TCP server)
local dt
    srv=net.createServer(net.TCP)
    srv:listen(5666,function(conn) --port 5666
      conn:on("receive",function(conn,payload)
        print(payload)
   if (string.find(payload, "X") ~= nil) then
            print("Get Nist time")
Same call-->   chunk = assert(loadfile("daytime_X.lua"))
Same call-->   chunk()
Same call-->   dt = foo()
      print("dt: " .. dt)
            tosend =  'you sent: ' .. payload .. " I sent date/time: " .. dt
   else
      tosend =  'you sent: ' .. payload
   end
   
        conn:send(tosend) -- plain text
      end)
   conn:on("sent",function(conn) conn:close() conn = nil end)
    end)

So, assert(loadfile ... works by itself, but not when combined with other code. The program flow never even gets to the socket stuff in daytime_X.lua before the error is thrown and it doesn't seem to be related. Sorry for the long post. I guess what I am also asking is how to pass variables between scripts. I am a Lua beginner so any suggestions are gratefully appreciated.