Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of broadcast Communication based on Cramp S Architecture under QT Network programming UDP

Shulou Source: shulou.com Published: 2022-06-03 13:18:04 10月02日 Update

Today, I will talk to you about the example analysis of broadcast communication under UDP under QT network programming. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

QT has the encapsulated UDP protocol class, QUdpSocket, which has the function interface we want. If you are interested, you can have a look.

Let's start with the server, write a subclass, inherit the QDialog class, and name it the UdpServer class. The header file should refer to the QUdpSocket class we mentioned above, as well as the class of the layout we want.

# ifndef UDPSERVER_H#define UDPSERVER_H#include # include class UdpServer: public QDialog {Q_OBJECTpublic: UdpServer (QWidget * parent = 0); ~ UdpServer (); private: QLabel * TimerLabel; QLineEdit * TextLineEdit; QPushButton* StartBtn; QVBoxLayout * mainLayout; public slots: void StartBtnClicked (); void timeout (); private: int port; bool isStarted; QUdpSocket * udpSocket; QTimer * timer;}; # endif / / UDPSERVER_H

In the .cpp file, we first display the interface, and then use udp's writedategram to write in what we want to upload.

# include "udpserver.h" UdpServer::UdpServer (QWidget * parent,Qt::WindowFlags f): QDialog (parent,f) {setWindowTitle (tr ("UDP SERVER")); TimerLabel = new QLabel ("show time:"), this); TextLineEdit = new QLineEdit (this); StartBtn = new QPushButton (tr ("start"), this); mainLayout = new QVBoxLayout (this); mainLayout- > addWidget (TimerLabel); mainLayout- > addWidget (TextLineEdit); mainLayout- > addWidget (StartBtn) Connect (StartBtn,SIGNAL (clicked ()), this,SLOT (StartBtnClicked (); port = 5555; isStarted = false; udpSocket = new QUdpSocket (this); timer = new QTimer (this); connect (timer,SIGNAL (timeout ()), this,SLOT (timeout ());} UdpServer::~UdpServer () {} void UdpServer::StartBtnClicked () {if (! isStarted) {StartBtn- > setText (tr ("STOP")); timer- > start (1000); isStarted = true } else {StartBtn- > setText (tr ("BEGIN")); isStarted = false; timer- > stop ();}} void UdpServer::timeout () {QString msg= TextLineEdit- > text (); int length=0; if (msg== "") {return;} if ((length=udpSocket- > writeDatagram (msg.toLatin1 (), msg.length (), QHostAddress::Broadcast,port)! = msg.length ()) {qDebug () addWidget (ReceiceTextEdit); mainLayout- > addWidget (CloseBtn) Connect (CloseBtn,SIGNAL (clicked ()), this,SLOT (CloseBtnClicked ()); port = 5555; udpSocket = new QUdpSocket (this); bool result = udpSocket- > bind (port); if (! result) {QMessageBox::information (this,tr ("ERROR"), tr ("connect error")); return;} connect (udpSocket,SIGNAL (readyRead ()), this,SLOT (dataReceived ();} UdpClient:: ~ UdpClient () {} void UdpClient::CloseBtnClicked () {close () } void UdpClient::dataReceived () {while (udpSocket- > hasPendingDatagrams ()) {QByteArray datagram; datagram.resize (udpSocket- > pendingDatagramSize ()); udpSocket- > readDatagram (datagram.data (), datagram.size ()); QString msg=datagram.data (); ReceiceTextEdit- > insertPlainText (msg);}}

Finally, the interface is displayed, and the server sends hello.

Received by the client:

Keep printing hello. Until you click close, or the server stops.

After reading the above, do you have any further understanding of the example analysis of broadcast communication under QT network programming UDP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Content customer client service architecture example network network programming analysis broadcast programming communication function layout file interface above thing interest subclass Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Shulou Information NVidia Shulou Technology