1.QT使用udp通信
1. Qt6使用udp协议在客户端发送一条文字消息到服务器端。
编写两个使用qt6框架开发的程序一个是服务端一个是客户端,单独使用cmake管理项目,Qt6使用udp协议在客户端发送一条文字消息到服务器端,无ui界面,按照qt风格分开声明与实现。
1.udpserver
main.cpp
1
2
3
4
5
6
7
8
9
10
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpServer server;
return a.exec();
}udpserver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class UdpServer : public QObject
{
Q_OBJECT
public:
UdpServer();
private slots:
void slotReadyRead();
private:
QUdpSocket *socket;
};udpserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
UdpServer::UdpServer()
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::AnyIPv4, 8848);
connect(socket, &QUdpSocket::readyRead, this, &UdpServer::slotReadyRead);
}
void UdpServer::slotReadyRead()
{
QByteArray buffer;
QHostAddress sender;
quint16 senderPort;
buffer.resize(socket->pendingDatagramSize());
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
qDebug() << "Received from" << sender.toString() << ":" << senderPort;
qDebug() << buffer;
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27cmake_minimum_required(VERSION 3.16)
project(udpserver LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
add_executable(udpserver
main.cpp
udpserver.h udpserver.cpp
)
target_link_libraries(udpserver Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpserver
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
2.udpclient
main.cpp
1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpClient client;
client.sendMessage("Hello from UDP client!");
return 0;
}udpclient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class UdpClient : public QObject
{
Q_OBJECT
public:
UdpClient();
void sendMessage(const QString &message);
private:
QUdpSocket *socket;
};udpclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
UdpClient::UdpClient()
{
socket = new QUdpSocket(this);
}
void UdpClient::sendMessage(const QString &message)
{
QByteArray data = message.toUtf8();
socket->writeDatagram(data, QHostAddress("127.0.0.1"), 8848);
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29cmake_minimum_required(VERSION 3.16)
project(udpclient LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core NetWork)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core NetWork)
add_executable(udpclient
main.cpp
udpclient.h udpclient.cpp
)
target_link_libraries(udpclient Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpclient
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
2. Qt6使用udp协议在客户端发送多条文字消息到服务器端。
编写两个使用qt6框架开发的程序一个是服务端一个是客户端,单独使用cmake管理项目,Qt6使用udp协议在客户端发送多条文字消息到服务器端,无ui界面,按照qt风格分开声明与实现,参考qwiget模板但是没有ui。
1.udpserver
main.cpp
1
2
3
4
5
6
7
8
9
10
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpServer server;
return a.exec();
}udpserver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class UdpServer : public QObject
{
Q_OBJECT
public:
explicit UdpServer(QObject *parent = nullptr);
private slots:
void slotReadyRead();
private:
QUdpSocket *socket;
};udpserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
UdpServer::UdpServer(QObject *parent)
: QObject(parent),
socket(new QUdpSocket(this))
{
// 绑定到任意 IPv4 地址的 8848 端口
if (socket->bind(QHostAddress::AnyIPv4, 8848)) {
qDebug() << "UDP Server started on port 8848";
} else {
qDebug() << "Failed to bind UDP Server";
}
connect(socket, &QUdpSocket::readyRead,
this, &UdpServer::slotReadyRead);
}
void UdpServer::slotReadyRead()
{
while (socket->hasPendingDatagrams()) {
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
qDebug() << "Received from" << sender.toString() << ":" << senderPort
<< "->" << QString::fromUtf8(buffer);
}
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27cmake_minimum_required(VERSION 3.16)
project(udpserver LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
add_executable(udpserver
main.cpp
udpserver.h udpserver.cpp
)
target_link_libraries(udpserver Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpserver
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
2.udpclient
main.cpp
1
2
3
4
5
6
7
8
9
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpClient client;
return a.exec();
}udpclient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class UdpClient : public QObject
{
Q_OBJECT
public:
explicit UdpClient(QObject *parent = nullptr);
private slots:
void sendNextMessage();
private:
QUdpSocket *socket;
QStringList messages;
int currentIndex;
QTimer timer;
};udpclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
UdpClient::UdpClient(QObject *parent)
: QObject(parent),
socket(new QUdpSocket(this)),
currentIndex(0)
{
// 准备要发送的多条消息
messages << "Hello Server!"
<< "This is message 2"
<< "Qt6 UDP test"
<< "Goodbye!";
connect(&timer, &QTimer::timeout,
this, &UdpClient::sendNextMessage);
timer.start(1000); // 每秒发送一条
}
void UdpClient::sendNextMessage()
{
if (currentIndex >= messages.size()) {
qDebug() << "All messages sent.";
timer.stop();
return;
}
QByteArray data = messages[currentIndex].toUtf8();
socket->writeDatagram(data, QHostAddress("127.0.0.1"), 8848);
qDebug() << "Sent:" << messages[currentIndex];
currentIndex++;
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29cmake_minimum_required(VERSION 3.16)
project(udpclient LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core NetWork)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core NetWork)
add_executable(udpclient
main.cpp
udpclient.h udpclient.cpp
)
target_link_libraries(udpclient Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpclient
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
3.Qt6使用udp协议在客户端发送到服务器后返回消息。
编写两个使用qt6框架开发的程序一个是服务端一个是客户端,Qt6使用udp协议在客户端发送到服务器后返回消息,无ui界面,按照qt风格分开声明与实现,参考qwiget模板但是没有ui。
1.udpserver
main.cpp
1
2
3
4
5
6
7
8
9
10
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpServer server;
return a.exec();
}udpserver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class UdpServer : public QObject
{
Q_OBJECT
public:
explicit UdpServer(QObject *parent = nullptr);
private slots:
void slotReadyRead();
private:
QUdpSocket *socket;
};udpserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
UdpServer::UdpServer(QObject *parent)
: QObject(parent),
socket(new QUdpSocket(this))
{
if (socket->bind(QHostAddress::AnyIPv4, 8848)) {
qDebug() << "UDP Server started on port 8848";
} else {
qDebug() << "Failed to bind UDP Server";
}
connect(socket, &QUdpSocket::readyRead,
this, &UdpServer::slotReadyRead);
}
void UdpServer::slotReadyRead()
{
while (socket->hasPendingDatagrams()) {
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
qDebug() << "Received from" << sender.toString() << ":" << senderPort
<< "->" << QString::fromUtf8(buffer);
// 回发消息
QByteArray reply = "Server received: " + buffer;
socket->writeDatagram(reply, sender, senderPort);
}
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27cmake_minimum_required(VERSION 3.16)
project(udpserver LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
add_executable(udpserver
main.cpp
udpserver.h udpserver.cpp
)
target_link_libraries(udpserver Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpserver
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
2.udpclient
main.cpp
1
2
3
4
5
6
7
8
9
10
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpClient client;
return a.exec();
}udpclient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class UdpClient : public QObject
{
Q_OBJECT
public:
explicit UdpClient(QObject *parent = nullptr);
private slots:
void sendNextMessage();
void slotReadyRead();
private:
QUdpSocket *socket;
QStringList messages;
int currentIndex;
QTimer timer;
};udpclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
UdpClient::UdpClient(QObject *parent)
: QObject(parent),
socket(new QUdpSocket(this)),
currentIndex(0)
{
messages << "Hello Server!"
<< "This is message 2"
<< "Qt6 UDP Echo Test"
<< "Goodbye!";
connect(&timer, &QTimer::timeout,
this, &UdpClient::sendNextMessage);
connect(socket, &QUdpSocket::readyRead,
this, &UdpClient::slotReadyRead);
timer.start(1000); // 每秒发送一条
}
void UdpClient::sendNextMessage()
{
if (currentIndex >= messages.size()) {
qDebug() << "All messages sent.";
timer.stop();
return;
}
QByteArray data = messages[currentIndex].toUtf8();
socket->writeDatagram(data, QHostAddress("127.0.0.1"), 8848);
qDebug() << "Sent:" << messages[currentIndex];
currentIndex++;
}
void UdpClient::slotReadyRead()
{
while (socket->hasPendingDatagrams()) {
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
qDebug() << "Reply from" << sender.toString() << ":" << senderPort
<< "->" << QString::fromUtf8(buffer);
}
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29cmake_minimum_required(VERSION 3.16)
project(udpclient LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core NetWork)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core NetWork)
add_executable(udpclient
main.cpp
udpclient.h udpclient.cpp
)
target_link_libraries(udpclient Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpclient
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
4.Qt6使用udp协议与ipv6地址在客户端发送一条文字消息到服务器端。
Qt6使用udp协议与ipv6地址在客户端发送一条文字消息到服务器端。
1.udpserver
main.cpp
1
2
3
4
5
6
7
8
9
10
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpServer server;
return a.exec();
}udpserver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class UdpServer : public QObject
{
Q_OBJECT
public:
explicit UdpServer(QObject *parent = nullptr);
private slots:
void slotReadyRead();
private:
QUdpSocket *socket;
};udpserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
UdpServer::UdpServer(QObject *parent)
: QObject(parent),
socket(new QUdpSocket(this))
{
// 绑定到 IPv6 任意地址,端口 8848
if (socket->bind(QHostAddress::AnyIPv6, 8848)) {
qDebug() << "UDP IPv6 Server started on port 8848";
} else {
qDebug() << "Failed to bind UDP IPv6 Server";
}
connect(socket, &QUdpSocket::readyRead,
this, &UdpServer::slotReadyRead);
}
void UdpServer::slotReadyRead()
{
while (socket->hasPendingDatagrams()) {
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
qDebug() << "Received from" << sender.toString() << ":" << senderPort
<< "->" << QString::fromUtf8(buffer);
}
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27cmake_minimum_required(VERSION 3.16)
project(udpserver LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
add_executable(udpserver
main.cpp
udpserver.h udpserver.cpp
)
target_link_libraries(udpserver Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpserver
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
2.udpclient
main.cpp
1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UdpClient client;
client.sendMessage("Hello from IPv6 UDP client!");
return 0;
}udpclient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class UdpClient : public QObject
{
Q_OBJECT
public:
explicit UdpClient(QObject *parent = nullptr);
void sendMessage(const QString &message);
private:
QUdpSocket *socket;
};udpclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
UdpClient::UdpClient(QObject *parent)
: QObject(parent),
socket(new QUdpSocket(this))
{
}
void UdpClient::sendMessage(const QString &message)
{
QByteArray data = message.toUtf8();
// IPv6 回环地址 ::1
QHostAddress serverAddress("::1");
socket->writeDatagram(data, serverAddress, 8848);
qDebug() << "Sent to" << serverAddress.toString() << ":" << 8848
<< "->" << message;
}CmakeList.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29cmake_minimum_required(VERSION 3.16)
project(udpclient LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core NetWork)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core NetWork)
add_executable(udpclient
main.cpp
udpclient.h udpclient.cpp
)
target_link_libraries(udpclient Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network)
include(GNUInstallDirs)
install(TARGETS udpclient
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
