#-------------------------------------------------
#
# Project created by QtCreator 2018-12-09T16:01:34
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = udpclient
TEMPLATE = app
SOURCES += main.cpp\
udpclient.cpp
HEADERS += udpclient.h
FORMS += udpclient.ui
服务器端
#include "udpserver.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UdpServer w;
w.show();
return a.exec();
}
#ifndef UDPSERVER_H
#define UDPSERVER_H
#include <QtWidgets>
#include <QUdpSocket>
namespace Ui {
class UdpServer;
}
class UdpServer : public QWidget
{
Q_OBJECT
public:
explicit UdpServer(QWidget *parent = 0);
~UdpServer();
private slots:
void readPendingDatagrams();
void on_sendBtn_clicked();
private:
Ui::UdpServer *ui;
QUdpSocket *udpServer;
QHostAddress sender;
quint16 senderPort;
};
#endif // UDPSERVER_H
#include "udpserver.h"
#include "ui_udpserver.h"
UdpServer::UdpServer(QWidget *parent) :
QWidget(parent),
ui(new Ui::UdpServer)
{
ui->setupUi(this);
udpServer = new QUdpSocket(this);
udpServer->bind(QHostAddress::LocalHost, 7755); //127.0.0.1
connect(udpServer, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
}
UdpServer::~UdpServer()
{
delete ui;
}
void UdpServer::readPendingDatagrams()
{
while (udpServer->hasPendingDatagrams()) { //如果缓冲区有至少一个数据包,那么返回真
QByteArray datagram;
datagram.resize(udpServer->pendingDatagramSize()); //重设接收缓冲区大小为待读取的字节数
udpServer->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
//业务逻辑
ui->recvEdit->setText(QString(datagram));
}
}
void UdpServer::on_sendBtn_clicked()
{
//给客户端发送一定要等待接收到客户端地址以后
udpServer->writeDatagram(ui->sendEdit->text().toUtf8(), sender, senderPort);
}
客户端
#include "udpclient.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UdpClient w;
w.show();
return a.exec();
}
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include <QtWidgets>
#include <QUdpSocket>
namespace Ui {
class UdpClient;
}
class UdpClient : public QWidget
{
Q_OBJECT
public:
explicit UdpClient(QWidget *parent = 0);
~UdpClient();
private slots:
void on_sendBtn_clicked();
void readPendingDatagrams();
private:
Ui::UdpClient *ui;
QUdpSocket *cliSocket;
};
#endif // UDPCLIENT_H
#include "udpclient.h"
#include "ui_udpclient.h"
UdpClient::UdpClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::UdpClient)
{
ui->setupUi(this);
cliSocket = new QUdpSocket(this);
connect(cliSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
}
UdpClient::~UdpClient()
{
delete ui;
}
void UdpClient::on_sendBtn_clicked()
{
cliSocket->writeDatagram(ui->sendEdit->text().toUtf8(), QHostAddress::LocalHost, 7755);
}
void UdpClient::readPendingDatagrams()
{
while (cliSocket->hasPendingDatagrams()) { //如果缓冲区有至少一个数据包,那么返回真
QByteArray datagram;
datagram.resize(cliSocket->pendingDatagramSize()); //重设接收缓冲区大小为待读取的字节数
cliSocket->readDatagram(datagram.data(), datagram.size(),
NULL, NULL);
//业务逻辑
ui->recvEdit->setText(QString(datagram));
}
}