QT5 下UDP 编程实例:客户端,服务器端

下面是一个简单的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);

}
  • 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.pro 中加入 :

QT += network 
这样就可以支持网络开发了。

在udpserver.h中添加想要的槽函数

public slots:
void StartBtnClicked();
void timeout();

privateint port;
bool isStarted;
QDdpSocket *udpSocket;
QTimer *timer;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在源文件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()));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

槽函数实现:

void UdpServer::StartBtnClicked()
{
if(!isStarted)
{
StartBtn->setText(tr("STOP");
timer->start(1000);//启动定时器
isStarted = true;
}
else
{
StartBtn->setText(tr("Start"));
isStarted = false;
timer->stop();
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

*——————————————————————————————————————————————————————————————–

下面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;

};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在UdpClient.pro中添加以下语句以支持网络开发: 
QT += network

在udpclient.h中添加以下代码:

public slots:
void CloseBtnClicked();
void dataReceived();

privateint port;
QUdpSocket *UDPSocket;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在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;

}


各个槽函数的实现
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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的简单通信例子,代码只给出了简单的一部分。布局可以自己看着实现。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值