-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
54 lines (49 loc) · 1.05 KB
/
example.lua
File metadata and controls
54 lines (49 loc) · 1.05 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local asyncio = require "asyncio"
local fiber = require "fiber"
local client = fiber:new(function()
local sock = asyncio.TCP:new()
print "Connecting..."
local ok, err = fiber.wait_for(5, function()
return sock:connect("127.0.0.1", 4040)
end)
if not ok then
print(err)
return
end
fiber.defer(function()
sock:close()
end)
local msg = ""
local byte, err
repeat
byte, err = sock:receive(1)
if byte then msg = msg .. byte end
until not byte
print("Message from server: " .. msg)
end)
local server = fiber:new(function()
local sock = asyncio.TCP:new()
fiber.defer(function()
sock:close()
end)
local ok, err = sock:bind("127.0.0.1", 4040)
if not ok then
print(err)
return
end
ok, err = sock:listen(16)
if not ok then
print(err)
return
end
while true do
print "Waiting for connection..."
local client = sock:accept()
fiber.spawn(function()
print "Client connected"
client:send "Hello, Client!"
client:close()
end)
end
end)
fiber.loop { client, server }