实现获取网络时间

脚本示例:

local sz = require("sz")
local socket = require "szocket.core"

server_ip = {
    "132.163.4.101",
    "132.163.4.102",
    "132.163.4.103",
    "128.138.140.44",
    "192.43.244.18",
    "131.107.1.10",
    "66.243.43.21",
    "216.200.93.8",
    "208.184.49.9",
    "207.126.98.204",
    "207.200.81.113",
    "205.188.185.33"
}
function nstol(str)
    assert(str and #str == 4)
    local t = {str:byte(1,-1)}
    local n = 0
    for k = 1, #t do
        n= n*256 + t[k]
    end
    return n
end
function gettime(ip)
    local tcp = socket.tcp()
    tcp:settimeout(10)
    tcp:connect(ip, 37)
    success, time = pcall(nstol, tcp:receive(4))
    tcp:close()
    return success and time or nil
end
function nettime()
    for _, ip in pairs(server_ip) do
        time = gettime(ip)
        if time then 
            return time
        end
    end
end
dialog(nettime(),0)
Copyright 北京帮你玩科技有限公司 2020 all right reserved,powered by Gitbook该文章修订时间: 2020-01-19 17:41:57

results matching ""

    No results matching ""