• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Lua > Lua获取网络时间(获取时间同步服务器的时间)

Lua获取网络时间(获取时间同步服务器的时间)

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了lua 服务器,lua服务器框架,lua获取当前时间,lua获取系统时间,lua 获取网络时间等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

网络授时服务是一些网络上的时间服务器提供的时间,一般用于本地时钟同步。 授时服务有很多种,一般我们选择RFC-868。这个协议的工作流程是:(S代表Server,C代表Client)

S: 检测端口37
U: 连接到端口37
S: 以32位二进制数发送时间
U: 接收时间
U: 关闭连接
S: 关闭连接
协议非常简单,用TCP连接上后,服务器直接把时间发送回来。发送的是从1900年1月1日午夜到现在的秒数。

使用luasocket
实现的方案有很多种,Lua不一定是最简单的,选择只是出于个人兴趣。直接上代码吧

-----------------------------------------------------------------------------
-- Network Time Protocal
-- Author: ani_di
-----------------------------------------------------------------------------
package.cpath = package.cpath .. ';D:\\tools\\Lua\\5.1\\clibs\\?.dll;?.dll'
local socket = require "socket.core"

server_ip = {
    -- "129.6.15.29",
    "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

-- get time from a ip address, use tcp protocl
function gettime(ip)
  print('connect ', 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
</div>


代码原理不细说,非常简单。唯一值得一提的是socket库包含。最开始用的这句 require "socket"

在解释器中表现很好,但在用C中调用会找不到相应的module。错误提示

  no field package.preload['socket']
  no file '.\socket.lua'
  no file 'F:\Projects\Lua\nettime\lua\socket.lua'
  no file 'F:\Projects\Lua\nettime\lua\socket\init.lua'
  no file 'F:\Projects\Lua\nettime\socket.lua'
  no file 'F:\Projects\Lua\nettime\socket\init.lua'
  no file 'D:\tools\Lua\5.1\lua\socket.luac'
  no file '.\socket.dll'
  no file '.\socket51.dll'
  no file 'F:\Projects\Lua\nettime\socket.dll'
  no file 'F:\Projects\Lua\nettime\socket51.dll'
  no file 'F:\Projects\Lua\nettime\clibs\socket.dll'
  no file 'F:\Projects\Lua\nettime\clibs\socket51.dll'
  no file 'F:\Projects\Lua\nettime\loadall.dll'
  no file 'F:\Projects\Lua\nettime\clibs\loadall.dll'.
</div>


网上也有好多类似的提问,大抵是没仔细看作者的Guide。显著的有这么一句

The other two environment variables instruct the compatibility module to look for dynamic libraries and extension modules in the appropriate directories and with the appropriate filename extensions.>

LUAPATH=/?.lua;?.lua LUACPATH=/?.dll;?.dll

至于"socket.core",windows默认安装位于“\socket\core.dll”。

C宿主调用

#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <time.h>
#include <Windows.h>

int load(lua_State* L, const char* func, unsigned int* utc) {
  lua_getglobal(L, func);
  if (lua_pcall(L, 0, 1, 0)) {
    printf("Error Msg pcall %s.\n", lua_tostring(L, -1));
    return -1;
  }

  if (!lua_isnumber(L,-1)) {
    printf("time should be a number\n" );
    return -2;
  }

  *utc = lua_tonumber(L,-1);
  lua_pop(L, -1);
  return 0;
}

void TimetToFileTime( time_t t, LPFILETIME pft )
{
  LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
  pft->dwLowDateTime = (DWORD) ll;
  pft->dwHighDateTime = ll >>32;
}

int main()
{
  lua_State* L = luaL_newstate();
  unsigned int utc = 0;
  luaL_openlibs(L);
 if (luaL_loadfile(L, "nettime.lua") || lua_pcall(L, 0, 0, 0)) {
    printf("Error Msg load %s.\n", lua_tostring(L, -1));
    return -1;
  }
  do {
  if(load(L,"nettime", &utc) == 0) {
    time_t tt = utc - 2208988800L;
    SYSTEMTIME st;
    FILETIME ft;
    TimetToFileTime(tt, &ft);
    if (FileTimeToSystemTime(&ft, &st))
    {
      printf("Today is: %d-%d-%d\n", st.wYear, st.wMonth, st.wDay);
      SetSystemTime(&st);
    }
    break;
  } else {
    puts("No network!");
    Sleep(10000);
  }
  } while (1);
  lua_close(L);
  return 0;
}
</div>

</div>

您可能想查找下面的文章:

  • Lua获取网络时间(获取时间同步服务器的时间)

相关文章

  • Lua极简入门指南(三): loadfile和错误处理
  • Lua字符串模式匹配函数小结
  • Lua中的模块与module函数详解
  • Lua中的loadfile、dofile、loadstring、require用法实例
  • 把Lua函数传递到C/C++中实例
  • openresty中使用lua-nginx创建socket实例
  • Lua中table库函数方法介绍
  • Lua中计算、执行字符串中Lua代码的方法
  • Lua中的弱引用介绍
  • Lua中break语句的使用方法详解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 详解Lua中的if语句的使用方法
    • Lua 学习笔记之C API 遍历 Table实现代码
    • Ruby类实例变量、类实例方法和类变量、类方法的区别
    • Lua中的string库和强大的模式匹配学习笔记
    • Lua中简单的错误处理实例
    • Lua cjson模块编译笔记及错误解决方法
    • Lua中的table学习笔记
    • Lua教程(三):值与类型介绍
    • Lua中遍历文件操作代码实例
    • Lua中的元表与元方法学习总结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有