-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_test_server.lua
47 lines (35 loc) · 1.13 KB
/
socket_test_server.lua
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
-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 65367))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
io.flush()
server:settimeout(60)
local client, err = server:accept()
print("after server accept", client, err)
io.flush()
client:settimeout(2)
print("after timeout")
io.flush()
local line, err = client:receive()
print("after client receive: " .. line)
io.flush()
client:close()
server:close()
-- loop forever waiting for clients
-- while 1 do
-- wait for a connection from any client
-- local client = server:accept()
-- make sure we don't block waiting for this client's line
-- client:settimeout(10)
-- receive the line
-- local line, err = client:receive()
-- if there was no error, send it back to the client
-- if not err then client:send(line .. "\n") end
-- done with client, close the object
-- client:close()
-- end