summaryrefslogtreecommitdiff
path: root/tests/modules/socket/tcp/echo_server.lua
blob: a047aa15f252c248257fd8fe0fc4dff668df26e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import("core.base.socket")

function main()
    local addr = "127.0.0.1"
    local port = 9001
    local sock = socket.bind(addr, port)
    sock:listen(20)
    local sock_client = nil
    while true do 
        print("%s: listening %s:%d ..", sock, addr, port) 
        local ok = sock:wait(socket.EV_ACPT, -1)
        if ok == socket.EV_ACPT then
            sock_client = sock:accept()
            if sock_client then
                print("%s: accepted", sock_client) 
                sock_client:close()
            end
        end
    end
    sock:close()
end