下面是一個簡單的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的簡單通信例子,代碼只給出了簡單的一部分。佈局可以自己看着實現。