赞
踩
下面是一个简单的QT5下的udp通信的下例子。服务器不停的利用定时器来向socket发送广播消息,客户端可以接收该消息并显示。
首先建立工程UdpServer.pro。建立各控件的布局。
- udpserver.h:
-
- class UdpServer:public QDialog
- {
- Q_OBJECT
- public:
- UdpServer(QWidget *parent=0,Qt::WindowFlags f=0);
- ~UdpServer();
-
- prvate:
- QLabel *TimerLabel;
- QLineEdit *TextLineEdit;
- QPushButton *StartBtn;
- QVBoxLayout *mainLayout;
- };
-
- udpserver.cpp
-
- UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f)
- {
- setWindowTitle(tr("UDP 服务器"));
-
- TimerLabel = new QLabel(tr("Timer:),this);
- TextLineEdit = new QLineEdit(this);
- StartBtn = new QPushButton(tr("Start"),this);
- mainLayout = new QVBoxLayout(this);
- mainLayout->addWidget(TimerLabel);
- mainLayout->addWidget(TextLineEdit);
- mainLayout->addWidget(StartBtn);
- }

下面实现服务器的消息广播功能:
在UdpServer.pro 中加入 :
QT += network
这样就可以支持网络开发了。
在udpserver.h中添加想要的槽函数
- public slots:
- void StartBtnClicked();
- void timeout();
-
- private:
- int port;
- bool isStarted;
- QDdpSocket *udpSocket;
- QTimer *timer;
在源文件udpserver.cpp的构造函数中添加如下代码
- conne(startBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked()));
- port = 5555;
- isStarted = false;
- UDPSocket = new QUdpSocket(this);//构建一个socket
- timer = new QTimer(this);//构建一个定时器
- connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
槽函数实现:
- void UdpServer::StartBtnClicked()
- {
- if(!isStarted)
- {
- StartBtn->setText(tr("STOP");
- timer->start(1000);//启动定时器
- isStarted = true;
- }
- else
- {
- StartBtn->setText(tr("Start"));
- isStarted = false;
- timer->stop();
- }
-
- }

timeout函数的实现广播消息:
- void Udpserver::timeout()
- {
- QString message = TextLineEdit->text();//获取要广播的消息文本
- int length = 0;//文本长度置零
- if(message= “”)//如果输入的文本为空,就返回
- {
- return;
- }
-
-
- /*向指定的地址和端口写入数据报消息,如果写入的长度和输入的长度不同,程序返回*/
- if((length = UDPSocket->writeDatagram(message.toLatin1(),message.length(),QHOSTAddress::Broadcast,port))!=message.length())
- {
- return;
- }
- }

*——————————————————————————————————————————————————————————————–
下面udp客户端的实现。获取服务器广播的消息,并显示出来。
首先建立工程 UdpClient.pro 建立各个控件的布局。
udpclient.h:
- class UdpClient:public QDialog
- {
- Q_OBJECT
- public:
- UdpClient(QWidget *parent =0,Qt::WindowFlags f=0);
- ~UdpClient();
-
- private:
- QTextEdit *ReceiveTextEdit;
- QPushButton *CloseBtn;
- QVBoxLayout *mainLayout;
-
- };
udpclient.cpp
- UdpClient::UdpClient(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f)
- {
- setWindowTitle(tr("Udp client"));
- ReceiveText = new QTextEdit(this);
- CloseBtn = new QPushButton(this);
-
- mainLayout = new QVBoxLayout(this);
- mainLayout->addWidget(ReceiveTextEdit);
- mainLayout->addWidget(CloseBtn);
- }
在UdpClient.pro中添加以下语句以支持网络开发:
QT += network
在udpclient.h中添加以下代码:
- public slots:
- void CloseBtnClicked();
- void dataReceived();
-
- private:
- int port;
- QUdpSocket *UDPSocket;
在udpclient.cpp构造函数中添加以下代码:
- connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked()));
-
- port = 5555;//端口号
-
- udpSocket = new QUdpSocket(this);//构建客户端的socket
- connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));//连接readyRead()信号与读取数据的槽
-
- bool result = udpSocket->bind(port);//绑定到指定的端口号
- if(!result)//若绑定不成功,给出出错信息
- {
- QMessageBox::information(this,tr(“Error”),tr(“UDP Socket Error”));
- return;
-
- }
-
-
- 各个槽函数的实现

void UdpClient::CloseBtnClicked()//关闭socket
{
close();
}
void UdpClient::dataReceived()//接收数据并显示
{
while(udpSocket->hasPendingDatagram())//只要socket有数据到达就循环的读取显示
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
QString message = datagram.data();
ReceiveTextEdit->insertPlainText(message);
}
}
“`
————————————————————————————————————————————————————————————————–
以上就是一个简单的udp的简单通信例子,代码只给出了简单的一部分。布局可以自己看着实现。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。