Luasocket 服務器,客戶端簡單實例


server.lua

#!/usr/bin/lua

local socket = require("socket")

host, port = "127.0.0.1", 9090

server = assert(socket.bind(host, port))
ack = "ack\n"

while true
do
    print("server: waiting for client connection ...")
    control = assert(server:accept())
    while true
    do
        command, status = control:receive()
        if status == "closed" then
           break
        end
        print(command)
        control:send(ack)       -- 注意:發送的數據必須要有結束符才能發送成功
    end

end

client.lua

#! /usr/bin/lua

local socket = require("socket")
host, port = "127.0.0.1", 9090

client = assert(socket.connect(host, port))

client:send("GET\n")

while true
do
    local s, status, partial = client:receive()
    print(s)
    if status == "closed" then
       break
    end

    client:send("GET\n")
end

client:close()

 

https://www.cnblogs.com/rohens-hbg/p/9014471.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM