当前位置: 首页 > news >正文

网站的意义百度学术官网登录入口

网站的意义,百度学术官网登录入口,卡一卡二三免视频,wordpress模板网站标题重复完整源码,附工程下载,工程其实也就下面两个代码。 想在不能上网的服务器局域网中部署一个时间服务NTP,当然系统自带该服务,可以开启,本文只是分享一下该协议报文和能跑的源码。网上作为服务的源码不太常见,…

完整源码,附工程下载,工程其实也就下面两个代码。
想在不能上网的服务器局域网中部署一个时间服务NTP,当然系统自带该服务,可以开启,本文只是分享一下该协议报文和能跑的源码。网上作为服务的源码不太常见,能见到不一定能跑起来,缺胳膊少脚的,分享代码一定要分享全。
先来效果图:
开启程序后,让win系统来同步本机时间效果如下:

添加图片注释,不超过 140 字(可选)

用通用客户端GuerrillaNtp来读,也没有问题:
在这里插入图片描述

添加图片注释,不超过 140 字(可选)
Fuck Shut Up,Show me the code
源码如下:
运行入口:

namespace NTP
{internal class Program{static void Main(string[] args){Console.WriteLine("Hello, NTP(SNTP)!");Console.WriteLine("本程序占用 UDP 123 端口,如遇冲突,请关闭系统自带时间服务 W32Time(Windows Time)");new SNTPServer().Start();Console.ReadLine();}}
}

主程序代码:

using System.Net.Sockets;
using System.Net;
using System.Buffers.Binary;namespace NTP
{/// <summary>/// 简单网络时间协议服务器/// </summary>public class SNTPServer{int port = 123;             //服务端口,NTP默认端口123bool stopFlag = false;      //通知后台线程停止消息循环的标识Thread tdServer;            //服务器后台监听线程/// <summary>/// 初始化一个简单网络时间协议服务器/// </summary>public SNTPServer(): this(123) { }/// <summary>/// 使用指定参数初始化一个简单网络时间协议服务器/// </summary>/// <param name="port">服务端口</param>public SNTPServer(int port){this.port = port;}/// <summary>/// 获取和设置服务端口号/// </summary>public int Port{get { return this.port; }set { this.port = value; }}/// <summary>/// 启动服务器/// </summary>public void Start(){if (tdServer == null || (!tdServer.IsAlive)){tdServer = new Thread(bgWork);tdServer.IsBackground = true;stopFlag = false;tdServer.Start();}}/// <summary>/// 停止服务器/// </summary>public void Stop(){stopFlag = true;}private void bgWork(){IPEndPoint iep;UdpClient udpclient;try{iep = new IPEndPoint(IPAddress.Any, port);udpclient = new UdpClient(iep);while (!stopFlag){if (udpclient.Available > 0){Console.WriteLine("收到一个请求:" + iep.Address + " 端口:" + iep.Port);byte[] buffer;IPEndPoint remoteipEP = new IPEndPoint(IPAddress.Any, port);buffer = udpclient.Receive(ref remoteipEP);if (buffer.Length >= 48){var bytesReceive = buffer.AsSpan();Console.WriteLine("收到数据[" + buffer.Length + "]:" + BitConverter.ToString(buffer));//记录收到请求的时间DateTime ReceiveTimestamp = DateTime.Now;//对方请求发出的时间,拿来显示看看DateTime? OriginateTimestamp = Decode(BinaryPrimitives.ReadInt64BigEndian(bytesReceive[40..]));//传输的都是国际时间,这里显示方便转成北京时间Console.WriteLine("对方请求发出时间:" + OriginateTimestamp?.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fff"));var Mode = 4;var VersionNumber = 3;var LeapIndicator = 0;var Stratum = 4;//Stratum 0是最高层级,代表原子钟、GPS接收器或其他高精度的时间源。var Poll = 0x0A;//NTP客户端向远程服务器发送时间请求的间隔(以秒为单位)。var Precision = 0xE9;buffer = new byte[48];var bytes = buffer.AsSpan();//LI(Leap Indicator):长度为2比特,值为“11”时表示告警状态,时钟未被同步。为其他值时NTP本身不做处理。//VN(Version Number):长度为3比特,表示NTP的版本号,目前的最新版本为3。//Mode:长度为3比特,表示NTP的工作模式。不同的值所表示的含义分别是:0未定义、1表示主动对等体模式、2表示被动对等体模式、3表示客户模式、4表示服务器模式、5表示广播模式或组播模式、6表示此报文为NTP控制报文、7预留给内部使用。//0x1C = 00 011 100bytes[0] = (byte)(((uint)LeapIndicator << 6) | ((uint)VersionNumber << 3) | (uint)Mode);bytes[1] = (byte)Stratum;bytes[2] = (byte)Poll;bytes[3] = (byte)Precision;BinaryPrimitives.WriteInt32BigEndian(bytes[4..], Encode(TimeSpan.Zero));//RootDelay = 0; Root Delay‌是指从主参考时钟到NTP服务器之间的往返时间延迟。它表示从主参考时钟到NTP服务器再返回的总体时间,通常以毫秒为单位。BinaryPrimitives.WriteInt32BigEndian(bytes[8..], Encode(TimeSpan.Zero));//RootDispersion = 0; Root Dispersion‌是指本地时钟相对于主参考时钟的最大误差。它表示NTP服务器时钟与主参考时钟之间的最大时间偏差,通常以毫秒为单位。BinaryPrimitives.WriteUInt32BigEndian(bytes[12..], BitConverter.ToUInt32(new byte[] { 0x41, 0x43, 0x54, 0x53 }, 0));//ReferenceIdentifierBinaryPrimitives.WriteInt64BigEndian(bytes[16..], Encode(DateTime.Now.ToUniversalTime()));//ReferenceTimestamp  系统时钟最后一次被设定或更新的时间//对方请求发出的时间 转成datetime 再转字节 会造成精度损失,【请求端会不认该报文】,应该返回原装字节,																						  //BinaryPrimitives.WriteInt64BigEndian(bytes[24..], Encode(OriginateTimestamp));bytes[24] = bytesReceive[40];bytes[25] = bytesReceive[41];bytes[26] = bytesReceive[42];bytes[27] = bytesReceive[43];bytes[28] = bytesReceive[44];bytes[29] = bytesReceive[45];bytes[30] = bytesReceive[46];bytes[31] = bytesReceive[47];BinaryPrimitives.WriteInt64BigEndian(bytes[32..], Encode(ReceiveTimestamp.ToUniversalTime()));//ReceiveTimestampBinaryPrimitives.WriteInt64BigEndian(bytes[40..], Encode(DateTime.Now.ToUniversalTime()));//TransmitTimestampConsole.WriteLine("返回数据[" + buffer.Length + "]:" + BitConverter.ToString(buffer));//系统NTP真实返回报文示例//buffer = new byte[] { 0x1C,0x04,0x00,0xE9,0x00,0x00,0x21,0xAA,0x00,0x00,0x16,0x0C,0x34,0xE7,0x72,0xB7,0xEB,0x0E, 0x62, 0x72,0xF7,0xCF,0x33,0xAF,0xEB,0x0E, 0x66, 0x3E, 0x79, 0x8B,0xB0,0x00,0xEB,0x0E, 0x66, 0x3E, 0x97, 0xCE,0xE6,0x82,0xEB,0x0E, 0x66, 0x3E, 0x97, 0xCF,0x51,0xE2 };udpclient.Send(buffer, buffer.Length, remoteipEP);}}}}catch (Exception e){Console.WriteLine(e.ToString());}}private const double FACTOR = 1L << 32;static readonly DateTime epoch = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc) + TimeSpan.FromSeconds(1L << 32);static int Encode(TimeSpan time) => (int)(time.TotalSeconds * (1 << 16));static long Encode(DateTime? time) => time == null ? 0 : Convert.ToInt64((time.Value - epoch).TotalSeconds * (1L << 32));static DateTime? Decode(long bits) => bits == 0 ? null : epoch + TimeSpan.FromSeconds(bits / FACTOR);}
}

本代码亲自测试,木有问题.
工程下载:download.csdn.net/download/wudizhukk/90159750

http://www.mmbaike.com/news/78209.html

相关文章:

  • 国外免费网站服务器链接seo诊断
  • 个人兼职做网站超能搜索引擎系统网站
  • 一个网站 多个域名江苏企业网站建设
  • 哪家网站优化公司好seo技术外包 乐云践新专家
  • 千万不要进住建局关键词优化的作用
  • 淘宝客网站api沧州seo包年优化软件排名
  • wordpress设置禁止检查更新免费发布网站seo外链
  • 这种资源网站怎么做才赚钱如何创建个人网站免费
  • 珠海网络公司网站建设营销的方法手段有哪些
  • 常州外贸网站建设公司网站
  • 网站建设与管理专业是什么优化课程
  • 企业邮箱多少钱一年安卓系统优化app
  • 动漫制作专业就业方向seo交流论坛seo顾问
  • 小地方的旅游网站怎么做网页设计图
  • 做网站需要走公司吗营销对企业的重要性
  • 网站建设文档中国十大公关公司排名
  • 个人电脑可以做网站服务器吗如何在网上推广自己的公司
  • 在网络上做兼职的网站推广游戏赚钱的平台有哪些
  • 北京住房建设厅网站企业网络营销的模式有哪些
  • 阿里云建立wordpress做seo要投入什么
  • 公司企业logo设计博客网站seo
  • 如何在国外做网站免费网络推广软件
  • 商城网站平台怎么做搜索引擎的工作原理有哪些
  • 东莞做网站有哪些产品网络推广的方法
  • 专做蓝领招聘网站有哪些松原头条新闻今日新闻最新
  • 网站设计制作全网优惠seo的基本工作内容
  • 网站没备案可以访问吗百度代理公司怎么样
  • 苏州市吴江区住房和城乡建设局网站seo还有用吗
  • 兰州网站建设100专门的网页制作工具有
  • 做网站每年需付费吗推广普通话手抄报文字