#include "CNetwork.h"
#include <QUdpSocket>
#include <QDateTime>
#include <QHostInfo>
#include <QMessageBox>
#include <QDebug>
#include <time.h>
#include <windows.h>
CNetwork::CNetwork(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(verifyTime()));
}
void CNetwork::verifyTime()
{
// QString ntpHost = "202.120.2.101"; //use ntp server get network time
// QString ntpHost = "192.168.11.132";
QString ntpHost = ui.IPlineEdit->text();
QUdpSocket udp_sock;
udp_sock.connectToHost(ntpHost, 123);
//request server time
qint8 LI = 0;
qint8 VN = 3;
qint8 MODE = 3;
qint8 STRATUM = 0;
qint8 POLL = 4;
qint8 PREC = -6;
QDateTime epoch(QDate(1900, 1, 1));
qint32 second = quint32(epoch.secsTo(QDateTime::currentDateTime()));
////////////////////////////////////////////////////
QDateTime preTime = QDateTime::currentDateTime();
qDebug() << "currnet time :" << QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz");
qDebug() << "second :" << QString::number(second);
qint32 temp = 0;
QByteArray time_req(48, 0);
time_req[0] = (LI << 6) | (VN << 3) | (MODE);
time_req[1] = STRATUM;
time_req[2] = POLL;
time_req[3] = PREC & 0xff;
time_req[5] = 1;
time_req[9] = 1;
time_req[40] = (temp = (second & 0xff000000) >> 24);
temp = 0;
time_req[41] = (temp = (second & 0x00ff0000) >> 16);
temp = 0;
time_req[42] = (temp = (second & 0x0000ff00) >> 8);
temp = 0;
time_req[43] = ((second & 0x000000ff));
QDateTime cur_dt;
//block wait connect
if (udp_sock.waitForConnected(3000))
{
udp_sock.flush();
//request time
udp_sock.write(time_req);
QString s, tmp;
for (int i = 0; i < 48; i++)
{
tmp.sprintf("%.2X", (unsigned char)time_req[i]);
s.append(tmp);
tmp.sprintf(" ");
s.append(tmp);
}
qDebug() << s;
//block wait reply
if (udp_sock.waitForReadyRead(5000))
{
QByteArray new_time;
QDateTime epoch(QDate(1900, 1, 1));
QDateTime unix_start(QDate(1970, 1, 1));
if (udp_sock.pendingDatagramSize() == 0)
{
QMessageBox::information(this, "msg", "please ensure the NTP service's ip! ");
return;
}
while (udp_sock.hasPendingDatagrams())
{
new_time.resize(udp_sock.pendingDatagramSize());
udp_sock.read(new_time.data(), new_time.size());
}
QString s, tmp;
for (int i = 0; i < 48; i++)
{
tmp.sprintf("%.2X", (unsigned char)new_time[i]);
s.append(tmp);
tmp.sprintf(" ");
s.append(tmp);
}
qDebug() << "rece message: " << s;
QByteArray TransmitTimeStamp;
TransmitTimeStamp = new_time.right(8);
quint32 seconds = TransmitTimeStamp[0];
quint32 *a = &seconds;
quint8 temp = 0;
for (int j = 1; j <= 3; j++)
{
seconds = seconds << 8;
temp = TransmitTimeStamp[j];
seconds = seconds + temp;
}
///////////////
quint8 temp1 = TransmitTimeStamp[4];
float x = 1000*(temp1 / 16) * (1.0/16.0);
float y = 1000*(temp1 % 16) * (1.0 / 256.0);
qint64 msecTemp = x + y;
qDebug() << temp1;
qDebug() << x;
qDebug() << y;
qDebug() << msecTemp;
/////////////////////////////////////////
qDebug() << "seconds:" << QString::number(seconds);
QDateTime newTime = QDateTime::currentDateTime();
qint64 value = newTime.toMSecsSinceEpoch() - preTime.toMSecsSinceEpoch();
//cur_dt.setSecsSinceEpoch(seconds - epoch.secsTo(unix_start));
cur_dt.setMSecsSinceEpoch((seconds - epoch.secsTo(unix_start))*1000);
cur_dt = cur_dt.addMSecs(value/2+msecTemp);
QString str = cur_dt.toString("yyyy.MM.dd hh:mm:ss.zzz");
ui.ResultlineEdit->setText(str);
setSystemTime(cur_dt);
}
else
{
QMessageBox::information(this,"message","get ntp time is time out");
}
udp_sock.disconnectFromHost();
udp_sock.close();
}
else
{
QHostInfo info = QHostInfo::fromName(ntpHost);
QString ip_addr = info.addresses().first().toString();
QMessageBox::information(this, "message", "connect ntp server is error!");
}
}
bool CNetwork::setSystemTime(const QDateTime &dateTime) //修改本地系统的时间 *添加kernel32.lib
{
SYSTEMTIME st;
GetLocalTime(&st);
st.wYear = dateTime.date().year();
st.wMonth = dateTime.date().month();
st.wDay = dateTime.date().day();
st.wHour = dateTime.time().hour();
st.wMinute = dateTime.time().minute();
st.wSecond = dateTime.time().second();
// st.wDayOfWeek= 0;
st.wMilliseconds = dateTime.time().msec();
return SetLocalTime(&st);
}
没有合适的资源?快使用搜索试试~ 我知道了~
vs2015+qt5.8.0 NTP协议的实现源码
共52个文件
tlog:18个
obj:8个
cpp:5个
需积分: 19 32 下载量 105 浏览量
2017-09-12
11:25:34
上传
评论
收藏 12.89MB ZIP 举报
温馨提示
vs2015+qt5.8.0的开发环境,对NTP协议的网络客户端的实现。服务器用的是win7系统配成的时间服务器,单独的时间服务器经测试也可以!
资源推荐
资源详情
资源评论
收起资源包目录
CNetwork.zip (52个子文件)
CNetwork
.vs
CNetwork
v14
.suo 30KB
CNetwork.sln 952B
CNetwork
Release
CNetwork.Build.CppClean.log 1KB
qrc_CNetwork.obj 2KB
main.obj 2KB
CNetwork.log 2KB
CNetwork.obj 63KB
moc_CNetwork.obj 3KB
CNetwork.tlog
CL.write.1.tlog 1KB
CL.read.1.tlog 104KB
custombuild.read.1.tlog 552B
CNetwork.lastbuildstate 175B
CL.command.1.tlog 6KB
link.write.1.tlog 710B
link.command.1.tlog 2KB
link.read.1.tlog 4KB
custombuild.write.1.tlog 882B
custombuild.command.1.tlog 2KB
CNetwork.vcxproj 10KB
CNetwork.h 310B
CNetwork.vcxproj.user 633B
Resources
CNetwork.cpp 5KB
main.cpp 175B
GeneratedFiles
Release
moc_CNetwork.cpp 3KB
ui_CNetwork.h 4KB
qrc_CNetwork.cpp 1KB
Debug
moc_CNetwork.cpp 3KB
Debug
CNetwork.Build.CppClean.log 2KB
vc140.pdb 1.75MB
qrc_CNetwork.obj 5KB
main.obj 85KB
CNetwork.log 3KB
CNetwork.obj 231KB
moc_CNetwork.obj 88KB
CNetwork.tlog
CL.write.1.tlog 1KB
CL.read.1.tlog 103KB
custombuild.read.1.tlog 420B
CNetwork.lastbuildstate 161B
CL.command.1.tlog 5KB
link.write.1.tlog 646B
link.command.1.tlog 2KB
link.read.1.tlog 4KB
custombuild.write.1.tlog 614B
custombuild.command.1.tlog 2KB
CNetwork.ui 2KB
CNetwork.vcxproj.filters 3KB
CNetwork.qrc 68B
Win32
Release
CNetwork.exe 37KB
Debug
CNetwork.pdb 1.88MB
CNetwork.ilk 954KB
CNetwork.exe 92KB
CNetwork.sdf 42.38MB
共 52 条
- 1
资源评论
heathen5539
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功