blob: 8a1ea6c7d77a8f04fe2da413bb0f0c6fed1954aa (
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
|
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
while true do
local recv, data = sock_client:recv(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)
result:dump()
sock_client:close()
end
end
sock:close()
end
|