-
Notifications
You must be signed in to change notification settings - Fork 4
/
echoclient
executable file
·80 lines (61 loc) · 1.65 KB
/
echoclient
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env lua5.1
--[[
An example RPC-like echo client.
See ECHO.txt for usage.
echoclient host ctrl_port string1 string2....
request one port
connect to requested port
send/receive each string in turn
close echo port
close control port
]]
require"socket"
local host = arg[1]
local ctrl_port = arg[2]
if #arg < 3 then
print("Usage: " .. arg[0] .. " host ctrl_port string1 [string2 string3 ...]")
os.exit(0)
end
function connect(host, port)
local s = socket.tcp()
assert(s:settimeout(5))
assert(s:connect(host, port))
print("Connected to:", host, port, "from", s:getsockname())
return s
end
local function get_echo_port(ctrl_sock)
print"Get port..."
assert(ctrl_sock:send("GET\n"))
local port = assert(ctrl_sock:receive())
print("Got port:", port)
return tonumber(port)
end
local function send_strings(host, port)
print("Connect to echo port:", host, port)
local s = connect(host, port)
for i = 3, #arg do
print(i, "Send:", arg[i])
assert(s:send(arg[i].."\n"))
local data, err = assert(s:receive())
print(i, "Recv:", data)
end
return s
end
local function pause()
os.execute"sleep 1" -- pause so conntrack -L can show the expectations
end
print("Connect to broker...")
local ctrl_sock = connect(host, ctrl_port)
echo_port = get_echo_port(ctrl_sock)
ctrl_sock:close()
echo_sock1 = send_strings(host, echo_port, "first")
pause()
echo_sock2 = send_strings(host, echo_port, "second")
echo_sock2:close()
echo_sock1:close()
--[[
print("Use echo port as long as we can...")
while send_strings(host, echo_port, "... as long as we can") do
pause()
end
--]]