void AtomicClock::on_TimeUpdate_clicked()
{
QString ipAddress
= info.
addresses().
first().
toString();
ui->ipAddress->setText(ipAddress);
udpSocket->connectToHost("0.pool.ntp.org", 123);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));
}
void AtomicClock::readPendingDatagrams()
{
ui->statusLabel->setText("Reading...");
while(udpSocket->hasPendingDatagrams())
{
QByteArray buffer
(udpSocket
->pendingDatagramSize
(),
0);
udpSocket->readDatagram(buffer.data(), buffer.size());
stream >> newTime;
ui->lineEdit->setText(newTime);
}
}
void AtomicClock::connectSuccsess()
{
ui->time->setText("Connected");
udpSocket
->writeDatagram
(timeRequest,
QHostAddress("0.pool.ntp.org"),
123);
}
void AtomicClock::on_TimeUpdate_clicked()
{
QHostInfo info = QHostInfo::fromName("0.pool.ntp.org");
QString ipAddress = info.addresses().first().toString();
ui->ipAddress->setText(ipAddress);
udpSocket = new QUdpSocket(this);
udpSocket->connectToHost("0.pool.ntp.org", 123);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));
}
void AtomicClock::readPendingDatagrams()
{
ui->statusLabel->setText("Reading...");
while(udpSocket->hasPendingDatagrams())
{
QByteArray buffer(udpSocket->pendingDatagramSize(), 0);
udpSocket->readDatagram(buffer.data(), buffer.size());
QDataStream stream(buffer);
stream.setVersion(QDataStream::Qt_4_6);
QString newTime;
stream >> newTime;
ui->lineEdit->setText(newTime);
}
}
void AtomicClock::connectSuccsess()
{
ui->time->setText("Connected");
QByteArray timeRequest(48,'0');
QDataStream out(&timeRequest, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);
udpSocket->writeDatagram(timeRequest, QHostAddress("0.pool.ntp.org"), 123);
}
To copy to clipboard, switch view to plain text mode
Bookmarks