lua socket

首先要有相应的socket模块

lua socket_第1张图片

server.lua

socket = require("socket");
host = host or "127.0.0.1";
port = port or "8888";
server = assert(socket.bind(host, port));
ack = "I'm server , ack\n";
while 1 do
    print("server: waiting for client connection...");
    control = assert(server:accept());
    while 1 do 
        command,status = control:receive();
  	if status == "closed" then 
		break 
	end
        print(command);
        control:send(ack);
    end
end
client.lua

local socket = require("socket")

host = "127.0.0.1"
port = 8888

--打开一个TCP连接
c = assert (socket.connect (host, port))

c:send ("I'm client,GET \n")
i = 0 
while (true) do
 local s, status, partial = c:receive ()
 print(s)
 if status == "closed" then 
	break 
 end
 i = i + 1
 if i == 10 then
	print("我主动断开连接了")
	c:close(); -- 主动关闭连接
 end
 c:send ("GET \n")
end

c:close ()


lua socket_第2张图片


参考学习:

http://blog.163.com/hbu_lijian/blog/static/126129153201422023644730/

你可能感兴趣的:(#,Nginx-Lua)