summaryrefslogtreecommitdiff
path: root/tests/modules/socket/unix_tcp/echo_server.lua
blob: 0a9199273774d41e610f95a76f885038453ce053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import("core.base.bytes")
import("core.base.socket")

function main(addr)

    addr = addr or path.join(os.tmpdir(), "echo.socket")
    os.tryrm(addr)
    local sock = socket.bind_unix(addr)
    sock:listen(20)
    print("%s: listening %s ..", sock, addr)
    while true do
        local sock_client = sock:accept()
        if sock_client then
            print("%s: accepted", sock_client)
            local count = 0
            local result = nil
            local buff = bytes(8192)
            while true do
                local recv, data = sock_client:recv(buff, 13, {block = true})
                if recv > 0 then
                    result = data
                    sock_client:send(data, {block = true})
                    count = count + 1
                else
                    break
                end
            end
            print("%s: recv: %d, count: %d", sock_client, result and result:size() or 0, count)
            if result then
                result:dump()
            end
            sock_client:close()
        end
    end
    sock:close()
end