Qt 5.10 Book Examples
UdpClient.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // UdpClient.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 "UdpClient.h"
14 
15 // ----------------------------------------------------------------------
16 UdpClient::UdpClient(QWidget* pwgt /*=0*/) : QTextEdit(pwgt)
17 {
18  setWindowTitle("UdpClient");
19 
20  m_pudp = new QUdpSocket(this);
21  m_pudp->bind(2424);
22  connect(m_pudp, SIGNAL(readyRead()), SLOT(slotProcessDatagrams()));
23 }
24 
25 // ----------------------------------------------------------------------
26 void UdpClient::slotProcessDatagrams()
27 {
28  QByteArray baDatagram;
29  do {
30  baDatagram.resize(m_pudp->pendingDatagramSize());
31  m_pudp->readDatagram(baDatagram.data(), baDatagram.size());
32  } while(m_pudp->hasPendingDatagrams());
33 
34  QDateTime dateTime;
35  QDataStream in(&baDatagram, QIODevice::ReadOnly);
36  in.setVersion(QDataStream::Qt_5_3);
37  in >> dateTime;
38  append("Received:" + dateTime.toString());
39 }
40 
UdpClient(QWidget *pwgt=0)
Definition: UdpClient.cpp:16