LuaSocket学习之tcp服务端

 1 --=================================--
 2 socket = require ("socket")
 3 print(socket._VERSION)
 4 
 5 if not socket then
 6     print("load socket module failed.")
 7 else
 8     print("success load socket module.")
 9     local host = "192.168.18.83"
10     local port = "12345"
11     lua_server = assert(socket.bind(host, port,1024))
12     lua_server:settimeout(0)
13     print("Server Start " .. host .. ":" .. port)
14 end
15 
16 while 1 do
17     local client = lua_server:accept()
18      if client then
19         print("A client successfully connect!")
20     end
21     
22     local recvt, sendt, status = socket.select({client}, nil, 1)
23     if #recvt > 0 then
24         local receive, receive_status = client:receive()
25         if receive_status ~= "closed" then
26             if receive then
27                 assert(client:send("Client  Send : "..receive .. "\n"))
28                 print("Receive Client : " .. receive)
29             end
30         else
31             client:close() 
32             print("Client disconnect!") 
33         end
34     end
35 end
36 
37 
38 
39 
40 
41         

 

posted @ 2020-09-12 11:18  齐齐大佬998  阅读(1193)  评论(0编辑  收藏  举报