Qt 5.10 Book Examples
UdpServer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // UdpServer.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtNetwork>
12 #include <QtGui>
13 #include "UdpServer.h"
14 
15 // ----------------------------------------------------------------------
16 UdpServer::UdpServer(QWidget* pwgt /*=0*/) : QTextEdit(pwgt)
17 {
18  setWindowTitle("UdpServer");
19 
20  m_pudp = new QUdpSocket(this);
21 
22  QTimer* ptimer = new QTimer(this);
23  ptimer->setInterval(500);
24  ptimer->start();
25  connect(ptimer, SIGNAL(timeout()), SLOT(slotSendDatagram()));
26 }
27 
28 // ----------------------------------------------------------------------
29 void UdpServer::slotSendDatagram()
30 {
31  QByteArray baDatagram;
32  QDataStream out(&baDatagram, QIODevice::WriteOnly);
33  out.setVersion(QDataStream::Qt_5_3);
34  QDateTime dt = QDateTime::currentDateTime();
35  append("Sent:" + dt.toString());
36  out << dt;
37  m_pudp->writeDatagram(baDatagram, QHostAddress::LocalHost, 2424);
38 }
39 
UdpServer(QWidget *pwgt=0)
Definition: UdpServer.cpp:16