资源简介
公司有几台设备需要用NTP同步后验证时差是否在15ms以内,遂编一小工具实现此需求,代码很简单,适合新手以及对NTP不太了解的盆友观摩。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NTPServer;
using System.Net.Sockets;
using System.Net;
namespace NTPClient
{
public partial class Form1 : Form
{
const int LocalPort = 12345;
int remoteport = 1234;
static UdpClient _udpclient;
static NTPOperation _ntpOp = new NTPOperation();
void send()
{
}
public Form1()
{
InitializeComponent();
_udpclient = new UdpClient(LocalPort AddressFamily.InterNetwork);
_udpclient.Client.ReceiveTimeout = 5000;
}
private void Btn_Sync_Click(object sender EventArgs e)
{
NTPPackage temppkg = new NTPPackage();
_ntpOp.setPackge(temppkg);
long OriginateTimestamp = 0;
long ReceiveTimestamp = 0;
Console.WriteLine(“OriginateTimestamp:{0}“ OriginateTimestamp);
_ntpOp.setMode(3);
_ntpOp.setVN(3);
_ntpOp.setLI(0);
_ntpOp._ntp.Stratum = 1;
_ntpOp._ntp.Poll = 0x0a;
_ntpOp._ntp.Precision = 0xe9;
_ntpOp._ntp.rootdelay = 0;
_ntpOp._ntp.RootDispersion = 0;
_ntpOp._ntp.ReferenceIdentifier = BitConverter.ToUInt32(new byte[4] { 0x41 0x43 0x54 0x53 } 0);
_ntpOp._ntp.ReferenceTimestamp = DateTime.Now.ToFileTimeUtc();
_ntpOp._ntp.OriginateTimestamp = OriginateTimestamp;
_ntpOp._ntp.ReceiveTimestamp = ReceiveTimestamp;
_ntpOp._ntp.TransmitTimestamp = DateTime.Now.ToFileTimeUtc();
byte[] sndpkg = _ntpOp.getBytes();
IPAddress ipaddr = null;
if (!IPAddress.TryParse(TB_IP.Text out ipaddr))
{
var szaddr = Dns.GetHostAddresses(TB_IP.Text);
if (szaddr.Length > 0)
{
ipaddr = szaddr[0];
}
else
{
MessageBox.Show(“无效域名或IP地址“);
return;
}
}
remoteport = Convert.ToInt32(TB_RemotePort.Text);
IPEndPoint remoteip = new IPEndPoint(ipaddr remoteport);
UdpState state = new UdpState(_udpclient remoteip);
_udpclient.Send(sndpkg sndpkg.Length remoteip);
byte[] recv = null;
try
{
recv = _udpclient.Receive(ref remoteip);
}
catch(Exception ex)
{
MessageBox.Show(“访问失败,请确保IP地址和端口正确“);
return;
}
if(recv.Length >= 60)
{
long t4 = DateTime.Now.ToFileTimeUtc();
NTPPackage pkg =
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 17920 2017-07-24 09:26 NTPClient\NTPClient\bin\Debug\NTPClient.exe
文件 48640 2017-07-24 09:26 NTPClient\NTPClient\bin\Debug\NTPClient.pdb
文件 11600 2017-07-24 09:28 NTPClient\NTPClient\bin\Debug\NTPClient.vshost.exe
文件 490 2017-03-19 05:00 NTPClient\NTPClient\bin\Debug\NTPClient.vshost.exe.manifest
文件 5201 2017-07-21 16:13 NTPClient\NTPClient\Form1.cs
文件 12732 2017-07-21 16:03 NTPClient\NTPClient\Form1.Designer.cs
文件 5817 2017-07-21 16:03 NTPClient\NTPClient\Form1.resx
文件 3842 2017-07-21 15:32 NTPClient\NTPClient\NTPClient.csproj
文件 2962 2017-07-24 09:25 NTPClient\NTPClient\NTPOperation.cs
文件 815 2017-07-24 09:25 NTPClient\NTPClient\NTPPackage.cs
文件 5420 2017-07-24 08:25 NTPClient\NTPClient\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6377 2017-07-24 09:26 NTPClient\NTPClient\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 1597 2017-07-24 09:28 NTPClient\NTPClient\obj\x86\Debug\NTPClient.csproj.FileListAbsolute.txt
文件 975 2017-07-21 16:03 NTPClient\NTPClient\obj\x86\Debug\NTPClient.csproj.GenerateResource.Cache
文件 9174 2017-07-24 08:26 NTPClient\NTPClient\obj\x86\Debug\NTPClient.csprojResolveAssemblyReference.cache
文件 17920 2017-07-24 09:26 NTPClient\NTPClient\obj\x86\Debug\NTPClient.exe
文件 180 2017-07-21 16:03 NTPClient\NTPClient\obj\x86\Debug\NTPClient.Form1.resources
文件 48640 2017-07-24 09:26 NTPClient\NTPClient\obj\x86\Debug\NTPClient.pdb
文件 180 2017-07-21 15:32 NTPClient\NTPClient\obj\x86\Debug\NTPClient.Properties.Resources.resources
文件 2042 2017-07-24 08:26 NTPClient\NTPClient\Program.cs
文件 1350 2017-07-21 11:47 NTPClient\NTPClient\Properties\AssemblyInfo.cs
文件 2870 2017-07-21 11:47 NTPClient\NTPClient\Properties\Resources.Designer.cs
文件 5612 2017-07-21 11:47 NTPClient\NTPClient\Properties\Resources.resx
文件 1096 2017-07-21 11:47 NTPClient\NTPClient\Properties\Settings.Designer.cs
文件 249 2017-07-21 11:47 NTPClient\NTPClient\Properties\Settings.settings
文件 1620 2017-07-21 15:39 NTPClient\NTPClient\SysDateTime.cs
文件 626 2017-07-21 11:16 NTPClient\NTPClient\UdpState.cs
文件 869 2017-07-21 11:47 NTPClient\NTPClient.sln
..A..H. 25600 2017-07-24 08:01 NTPClient\NTPClient.suo
文件 8704 2017-07-24 08:09 NTPServer\NTPServer\bin\Debug\NTPServer.exe
............此处省略37个文件信息
评论
共有 条评论