Mongoose 开源库--SNTP 网络校时使用笔记

news2024/11/7 15:30:32

一、SNTP介绍

    SNTP是一种简单的网络时间协议,它可以通过网络协议同步计算机的时间,时间的精度不如NTP。

关于SNTP介绍,可以参考 

https://www.ionos.com/digitalguide/server/know-how/sntp-simple-network-time-protocol/

或者

https://timetoolsltd.com/ntp/sntp-overview/

该原文如下:

Simple Network Time Protocol (SNTP) is an Internet Protocol (IP) used to synchronize the clocks of networks of computers.


SNTP is a subset of the Network Time Protocol (NTP).
The latest version is SNTP v4.
It can synchronize seamlessly to full-blown NTP servers.
Originally developed for small computers and micro-controllers.
Requires less memory and processing power than NTP.
It is used in applications where precise clock synchronization is not critical.
Uses the TCP/IP protocol suite, UDP port 123.
SNTP is a subset of the full-blown Network Time Protocol (NTP).


Complex statistical algorithms have been developed for NTP in order to improve the accuracy of clock synchronization and to reduce clock drift. However, the algorithms consume considerable amounts of memory and processing power.


For many applications, particularly on smaller computers, the ultimate performance of NTP is not needed. For this reason, the Simple Network Time Protocol (SNTP) was developed to provide clock synchronization for less powerful computers, where the complexity of NTP is not required.


SNTP Protocol Suite
SNTP is based upon the TCP/IP protocol suite. It is an application layer time protocol, part of the Network Time Protocol base protocol. Along with NTP, SNTP communicates using the User Datagram Protocol (UDP). By default, UDP port 123 is used.


A common oversight with new SNTP installations is that the UDP port must be left open in any firewall to allow communications transfer. If a client cannot synchronize to a server, check that UDP port 123 is open in the firewall configuration.


SNTP can operate on IPv4 and IPv6 networks and is defined by RFC 4330.


How SNTP Works
The SNTP protocol is a client-server protocol based on the exchange of packets of timing information.


Generally, clients operate in a unicast mode. At periodic intervals, usually in the region of every 64 seconds, a SNTP client sends a request to a NTP server referencing its IP address. The server is generally synchronized to a hardware reference clock, such as GPS.


On receipt of a response from the server, the client can calculate the system time offset between its internal clock and the servers clock. Enhanced synchronization is achieved by calculating packet round trip delays.


If required, SNTP can be configured in Broadcast or Multicast modes. In this instance, a server continually broadcasts timestamp packets at periodic intervals. Clients continually listen for packets and, on receipt, adjust their system clocks accordingly.


The NTP and SNTP protocols are completely interoperable.


Any SNTP client can synchronize to any NTP server. This is primarily because the packets of information exchanged are identical.


The differences between the protocols does not lie in the information exchange, but what the algorithms actually do with the information in order to achieve synchronization.


The Differences Between NTP and SNTP
The main differences between NTP and SNTP are contained within the application.


NTP has developed complex statistical algorithms with calibration techniques, aimed at filtering small discrepancies, to achieve a very high degree of clock synchronization.


Fine time adjustments are made by tweaking the ‘tick’ rate of the host computers system clock. NTP attempts to make time corrections seamlessly by skewing time adjustments, to avoid stepped changes.


Redundancy is implemented by continuously monitoring multiple time references. Complex selection algorithms ascertain the most reliable and stable. Multiple sources of time, including a mixture of hardware clocks and network time sources can be monitored, aiding robustness.


There are also a number of security features implemented in NTP. Secure client-server authentication is achieved by implementing Symmetric Key Cryptography. This allows a client to be sure of the origin of received time-stamps alleviating spoofing.


In contrast, SNTP adopts a much simpler approach to network clock synchronization. Many of the complexities of the NTP algorithms have been removed or simplified.


Rather than skewing time, many SNTP clients step time. However, stepping time can cause problems with event ordering. For example, if time is stepped back between the generation of two transactions, they would not have a correctly sequenced order.


SNTP also lacks the ability to monitor and filter multiple time references. The protocol can generally only be configured to operate from a single source of time. It is not a good choice if multiple redundant time sources are implemented.


For simplicity, many SNTP clients do not implement secure authentication techniques, which can leave systems vulnerable to attack from malicious users.


Modern, powerful, computer systems can easily cope with the additional overheads required by NTP with no significant loss of performance. Therefore, NTP should be used for all timing applications, unless the larger foot-print and resource requirement is a real issue.


Where Can SNTP Be Used in Place of NTP
Simple Network Time Protocol (SNTP) allows computers with less processing power to implement clock synchronization.


Typically, it is used by small network devices, such as IP cameras, DVR’s, IP phones, routers and consumer devices.


A number of operating systems for small computers and workstations also use the SNTP protocol for simplicity and because of it’s smaller foot-print and lower resource requirements.


Essentially, SNTP offers a much lower-quality time synchronization solution than NTP. However, it is ideal where the requirements of accurate time, security and reliability are less of a priority and having an application that uses fewer resources is important.


NTP must be used where clock synchronization is of critical importance, such as financial transaction processing and Air Traffic Control.


Summary
Simple Network Time Protocol was developed for small computers and micro-controllers with limited memory and processing power.


It provides clock synchronization for applications that do not require the precision of full-blown NTP.


NTP is better suited to synchronizing large clusters of computers, such as corporate networks. The robustness of its calibration algorithms helps larger computer systems reliably maintain accurate time synchronization.

二、Mongoose 开源库-SNTP相关API

官方手册介绍相关的API只有2个,如下:

2462203ae407812592c88b232151ce62.png

// Connect SNTP server
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
                                      mg_event_handler_t fn, void *fn_data)


       
// Send time request to SNTP server
void mg_sntp_request(struct mg_connection *c)

三、测试代码

测试代码,如下:

#include "mongoose.h"




// SNTP client connection
static struct mg_connection *s_sntp_conn = NULL;


// SNTP client callback
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) 
{
  if (ev == MG_EV_SNTP_TIME) 
  {
    int64_t t = *(int64_t *) ev_data;
    MG_INFO(("Got SNTP time: %lld ms from epoch", t));
  } 
  else if (ev == MG_EV_CLOSE) 
  {
    s_sntp_conn = NULL;
  }
  (void) fn_data;
   (void) c;
}


// Called every 5 seconds. Increase that for production case.
static void timer_fn(void *arg) 
{
  struct mg_mgr *mgr = (struct mg_mgr *) arg;
  if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, "udp://edu.ntp.org.cn:123", sfn, NULL);
  if (s_sntp_conn != NULL) mg_sntp_request(s_sntp_conn);
}


int main(void) 
{
  struct mg_mgr mgr;        // Event manager
  mg_mgr_init(&mgr);        // Initialise event manager
  mg_log_set(MG_LL_DEBUG);  // Set log level
  mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr);
  for (;;) mg_mgr_poll(&mgr, 300);  // Infinite event loop
  mg_mgr_free(&mgr);                // Free manager resources
  return 0;
}

测试结果:

d0d75afa7ba15b4b1aef24b9e0c940e1.png

四、公共 NTP 网络时间服务器

来源:

https://dns.icoa.cn/ntp/

9632aabe92ada4dc0e85478972e02e93.png

de9e5799c76b641070077d5a89a04c98.png

3ef0c50ce5043a6b1b8ada2eb6a069b4.png

欢迎关注公众号:嵌入式学习与实践

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1165025.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

docker部署Jenkins(Jenkins+Gitlab+Maven实现CI/CD)

GitLab介绍 GitLab是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的Web服务,可通过Web界面进行访问公开的或者私人项目。它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。…

开发项目管理必备神器!10款好用的在线看板工具推荐!

在当今高速发展、竞争激烈的商业环境中,项目管理已经成为了许多组织的生存之道。项目管理可以有效地协调资源,监控进度,把握风险,并实现组织的策略目标。然而,传统的项目管理方法已经无法满足日益复杂和变化的工作需求…

框架安全-CVE 复现Apache ShiroApache Solr漏洞复现

文章目录 服务攻防-框架安全&CVE 复现&Apache Shiro&Apache Solr漏洞复现中间件列表常见开发框架Apache Shiro-组件框架安全暴露的安全问题漏洞复现Apache Shiro认证绕过漏洞(CVE-2020-1957)CVE-2020-11989验证绕过漏洞CVE_2016_4437 Shiro-…

JS动态转盘可手动设置份数与概率(详细介绍)

这个案例是我老师布置的一项作业&#xff0c;老师已详细讲解&#xff0c;本人分享给大家&#xff0c;详细为你们介绍如何实现。 我们转盘使用线段来实现 <!DOCTYPE html> <html> <head><meta charset"utf-8"><title></title>&l…

2023云栖大会:揭示未来科技的璀璨星辰

翻开科技世界的崭新篇章&#xff0c;2023年的云栖大会将在星光璀璨的杭州盛大开启。这一盛会&#xff0c;是科技创新的聚集地&#xff0c;也是前沿科技展示的殿堂。每年&#xff0c;无数优秀的科技人才和业内精英汇聚于此&#xff0c;共同探讨科技的未来趋势和人类发展的无限可…

编写shell脚本,利用mysqldump实现MySQL数据库分库分表备份

查看数据和数据表 mysql -uroot -p123456 -e show databases mysql -uroot -p123456 -e show tables from cb_d 删除头部Database和数据库自带的表 mysql -uroot -p123456 -e show databases -N | egrep -v "information_schema|mysql|performance_schema|sys"编写…

【Java 进阶篇】Java ServletContext详解:在Web应用中获取全局信息

在Java Web开发中&#xff0c;ServletContext是一个重要的概念&#xff0c;它允许我们在整个Web应用程序中共享信息和资源。本篇博客将深入探讨ServletContext的作用、如何获取它&#xff0c;以及如何在Web应用中使用它。无论您是刚刚入门的小白还是有一定经验的开发者&#xf…

QML WebEngineView 调用 JavaScript

作者: 一去、二三里 个人微信号: iwaleon 微信公众号: 高效程序员 在 QML 与 Web 混合开发时,除了使用 WebEngineView 加载网页之外,我们还可以在 QML 层运行 JavaScript 代码,这样就能更灵活地操作浏览器窗口和网页内容,从而实现丰富的交互功能了。例如:获取网页标题、…

【JavaEE初阶】 初识网络原理

文章目录 &#x1f332;网络发展史&#x1f6a9;独立模式&#x1f6a9;网络互连&#x1f4cc;局域网LAN&#x1f388;基于网线直连&#x1f388;基于集线器组建&#x1f388;基于交换机组建&#x1f388;基于交换机和路由器组建 &#x1f4cc;广域网WAN &#x1f340;网络通信基…

人工智能与脑机接口:开启人机融合的新时代

人工智能与脑机接口&#xff1a;开启人机融合的新时代 随着人工智能&#xff08;AI&#xff09;技术的飞速发展&#xff0c;我们正与一个全新的时代相遇——人工智能与脑机接口相融合的时代。这个时代将带来前所未有的变革&#xff0c;让人类与机器的交互方式发生根本性的改变。…

洒洒水阿萨阿萨

1. 多表查询 多表查询(也叫关联查询, 联结查询): 可以用于检索涉及到多个表的数据. 使用关联查询, 可以将两张或多张表中的数据通过某种关系联系在一起, 从而生成需要的结果集.前提条件: 这些一起查询的表之间它们之间一定是有关联关系.# 先熟悉一下三张表: -- 1. 员工表(11个…

微信小程序快速备案的通道揭秘方法

随着国家政策的调整&#xff0c;微信小程序备案已变得刻不容缓。传统备案路径较为繁琐&#xff0c;耗时较长&#xff0c;为解决此痛点&#xff0c;今天我们将揭示一个快速备案的新通道。 步骤1&#xff1a;探索智慧助手 打开微信&#xff0c;探索“智慧商家服务助手”公众号。…

阿里云OS系统Alibaba Cloud Linux 3系统的安全更新命令

给客户部署的服务&#xff0c;进入运维阶段&#xff0c;但是经常被客户监测到服务器漏洞&#xff0c;现在整理一下&#xff0c;服务器漏洞问题更新命令步骤。 服务器系统&#xff1a; 阿里云linux服务器&#xff1a;Alibaba Cloud Linux 3 漏洞类型和描述&#xff1a; #3214…

[ASP]校无忧在线考试系统 v3.7

校无忧在线考试系统采用互联网技术,快速搭建在线考试系统平台,全面实现了考试工作的网络化、无纸化、自动化。系统操作简单,题型丰富,广泛用于企事业单位,学校教育培训机构等在线考试,网络考试,在线考核…… 系统主要功能&#xff1a; 1、设置基本考试系统信息(开通/关闭考试等…

听GPT 讲Rust源代码--library/std(12)

题图来自 Decoding Rust: Everything You Need to Know About the Programming Language[1] File: rust/library/std/src/os/watchos/mod.rs 该文件&#xff08;rust/library/std/src/os/watchos/mod.rs&#xff09;的作用是为Rust标准库提供支持WatchOS操作系统的特定功能。 W…

【JVM经典面试题(五十二道)】

文章目录 JVM经典面试题&#xff08;五十二道&#xff09;引言1.什么是JVM 内存管理2.能说一下JVM的内存区域吗&#xff1f;3.说一下JDK1.6、1.7、1.8内存区域的变化&#xff1f;4.为什么使用元空间替代永久代作为方法区的实现&#xff1f;5.对象创建的过程了解吗&#xff1f;6…

这两天公司面了一个字节来的要求月薪23K,明显感觉他背了很多面试题...

最近有朋友去字节面试&#xff0c;面试前后进行了20天左右&#xff0c;包含4轮电话面试、1轮笔试、1轮主管视频面试、1轮hr视频面试。 据他所说&#xff0c;80%的人都会栽在第一轮面试&#xff0c;要不是他面试前做足准备&#xff0c;估计都坚持不完后面几轮面试。 其实&…

k8s-服务网格实战-入门Istio

istio-01.png 背景 终于进入大家都比较感兴趣的服务网格系列了&#xff0c;在前面已经讲解了&#xff1a; 如何部署应用到 kubernetes服务之间如何调用如何通过域名访问我们的服务如何使用 kubernetes 自带的配置 ConfigMap 基本上已经够我们开发一般规模的 web 应用了&#xf…

【克隆方法+深浅拷贝】

文章目录 前言Clonable 接口克隆方法代码的实现 浅拷贝深拷贝总结 前言 Clonable 接口 克隆方法代码的实现 //1.当类要调用克隆方法时&#xff0c;这个类要实现一个Cloneable接口 class Student implements Cloneable{public String name;public int age;public Student(Str…

软件测试/测试开发丨ChatGPT能否成为PPT最佳伴侣

点此获取更多相关资料 简介 PPT 已经渗透到我们的日常工作中&#xff0c;无论是工作汇报、商务报告、学术演讲、培训材料都常常要求编写一个正式的 PPT&#xff0c;协助完成一次汇报或一次演讲。PPT相比于传统文本的就是有布局、图片、动画效果等&#xff0c;可以给到观众更好…