Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5972991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:44:14+00:00 2026-05-22T20:44:14+00:00

I’m a bit confused and think it’s going to be an easy answer but

  • 0

I’m a bit confused and think it’s going to be an easy answer but my searches aren’t helping me much 🙁 I want to be able to do an skt:send anywhere. I could send it into OutToUser function as a parameter but I’m going to have a lot of different places I’ll want to do this at and feel that will get too messy. I tried storing it something like Server.connections[key] = skt where key is host and port but can’t figure out how to get host and port again later when I need it.

Any good solutions?

edit I get that it’s a scope issue but not seeing a good solution as I’m new to lua.

require "copas"
Server = {}
function Server:new()
    local object = {}
    setmetatable(object, { __index = Server })
    return object
end

function Server:init()
    function handler(skt, host, port)
        while true do
            data = skt:receive()
            if data == "quit" then
                -- isn't going to work
                OutToUser(data)

                -- this would work fine of course
                -- skt:send(data .. "\r\n")
            end
        end
    end

    server = socket.bind("*", 49796)
    copas.addserver(server, 
        function(c) return handler(copas.wrap(c), c:getpeername()) end
    )
    copas.loop()
end

function OutToUser(data)
    skt:send(data .. "\r\n")
end

server = Server:new()
server:init()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:44:14+00:00Added an answer on May 22, 2026 at 8:44 pm

    You can define OutToUser in the scope of the handler:

    function Server:init()
        local function handler(skt, host, port)
    
            --make the function local to here
            local function OutToUser(data)
                --references the skt variable in the enclosing scope
                --(the handler function)
                skt:send(data .. "\r\n")
            end
    
            while true do
                data = skt:receive()
                if data == "quit" then
                    OutToUser(data)
                end
            end
        end
    
        local server = socket.bind("*", 49796)
        copas.addserver(server, 
            function(c) return handler(copas.wrap(c), c:getpeername()) end
        )
        copas.loop()
    end
    

    Functions can always reference variables in their scope (function arguments and variables declared with local), even once they’ve left that scope – you can use that as an alternative solution, where you enclose the variables you want the function to use in a scope outside the function:

    local function makeOTU(skt)
    
        --skt is visible in the scope of the function
        --that gets returned as a result
    
        return function(data)
            skt:send(data .. "\r\n")
        end
    end
    
    function Server:init()
        local function handler(skt, host, port)
    
        --create a function that references skt
        --as part of its closure
        local OutToUser = makeOTU(skt)
    
            while true do
                data = skt:receive()
                if data == "quit" then
                    -- OutToUser is still referencing the
                    -- skt from the call to makeOTU()
                    OutToUser(data)
                end
            end
        end
    
        local server = socket.bind("*", 49796)
        copas.addserver(server, 
            function(c) return handler(copas.wrap(c), c:getpeername()) end
        )
        copas.loop()
    end
    

    Note the use of the local keyword in both of these examples: if you neglect the local, the name will ignore the scope altogether and go into / come from the global environment (which is just a table like any other: when you invoke a new Lua state, it’s placed in the global _G), which is not what you want.

    Keeping your variables local to their scope instead of using globals is important. Take, for example, these two functions:

    local function yakkity(file, message)
    
        line = message .. '\n' --without local,
                               --equivalent to _G["line"] = message
    
        function yak() --without local,
                       --equivalent to _G["yak"] = function()
    
            file:write(line) --since no local "line" is defined above,
                             --equivalent to file:write(_G["line"])
        end
        for i=1, 5 do
            yak()
        end
    end
    
    local function yakker(file, message)
        line = message .. '\n' --without local,
                               --equivalent to _G["line"] = message
    
        return function()
            file:write(line) --again, since no local "line" is defined above,
                             --equivalent to file:write(_G["line"])
        end
    end
    

    Because their variables aren’t defined as local, they clobber each other’s data, leave their belongings lying around where anybody can abuse them, and just generally act like slobs:

    --open handles for two files we want:
    local yesfile = io.open ("yesyes.txt","w")
    local nofile = io.open ("no.txt","w")
    
    --get a function to print "yes!" - or, rather,
    --print the value of _G["line"], which we've defined to "yes!".
    --We'll see how long that lasts...
    local write_yes = yakker(yesfile,"yes!")
    
    --Calling write_yes() now will write "yes!" to our file.
    write_yes()
    
    --when we call yakkity, though, it redefines the global value of "line"
    --(_G["line"]) - as well as defining its yak() function globally!
    --So, while this function call does its job...
    yakkity(nofile, "oh no!")
    
    --calling write_yes(), which once again looks up the value of _G["line"],
    --now does the exact OPPOSITE of what it's supposed to-
    write_yes() --this writes "oh no!" to yesfile!
    
    --additionally, we can still write to the nofile handle we passed to yakkity()
    --by calling the globally-defined yak() function!
    yak() --this writes a sixth "oh no!" to nofile!
    
    --even once we're done with the file and close our handle to it...
    nofile:close()
    
    --yak() still refers to that handle and will still try to write to it!
    yak() --tries to write to the closed file handle and throws an error!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.