Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

UDP communication code and interpretation of qt

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

# include "server.h"

# include "ui_server.h"

Server::Server (QWidget * parent):

QWidget (parent)

Ui (new Ui::Server)

{

Port = 666,

Socket = new QUdpSocket (this); contains the QUdpSocket class in the .h file, and adds a pointer to the project where network,socket is defined in the .h file

/ / if the binding mode is different according to the system, select the mode through conditional compilation

# ifdef Q_OS_LINUX

Socket- > bind (port, QUdpSocket::ShareAddress)

# else / / Q_OS_WIN32

Socket- > bind (port, QUdpSocket::ReuseAddressHint)

# endif

Connect (socket, SIGNAL (readyRead ()

This, SLOT (readPendingDatagrams ())

Ui- > setupUi (this)

}

Server::~Server ()

{

Delete ui

}

Void Server::readPendingDatagrams () / / start reading data

{

While (socket- > hasPendingDatagrams ())

{

QByteArray datagram

Datagram.resize (socket- > pendingDatagramSize ()); / / obtain the length of the packet through socket

Socket- > readDatagram (datagram.data (), datagram.size ())

DecodeMessage (datagram); / / parse the message

}

}

Void Server::decodeMessage (const QByteArray & ba)

{

QDataStream stream (ba)

QString nick, message

Stream > > nick > > message; / / first read out the first string, assigned to nick (that is, the name), and the second to the message content

ShowMessage (nick, message)

}

/ / used to package messages

QByteArray Server::encodeMessage (const QString & nick, const QString & message) const

{

QByteArray ba

QDataStream stream (& ba, QIODevice::WriteOnly)

Stream text (), / / get the user name

Ui- > textEdit- > toPlainText (); / / get the message

Socket- > writeDatagram (ba, QHostAddress::Broadcast, port); / / initialize Datagram

}

Void Server::showMessage (const QString & nick, const QString & message)

{

QString line = tr ("% 1 says:% 2") .arg (nick) .arg (message)

Ui- > textBrowser- > append (line)

Ui- > textEdit- > clear ()

}

Void Server::on_pushButton_clicked ()

{

SendMessage ()

}

Here is only the .cpp file, which is the logical part of the core. As for the rest, I hope the reader will finish it on his own.

The name of the class used above is Server. In fact, the communication between the two machines does not need a server, as long as it is within a network segment.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report