[QT]QT教程之实例分析[九] 网络广播程序(QUdpSocket)

重点知识已近在代码里注释...
请仔细看代码
本文原创
转载请保留此链接  http://blog.csdn.net/siren0203

 

本章内容 以一个完整的 广播程序为实例 再次详细说明 UDP协议的使用方法..

 

发送端...

头文件

udpSocket.h

#ifndef UPDSOCKET_H #define UPDSOCKET_H #include <QDialog> class QUdpSocket; class QTextEdit; class QVBoxLayout; class Server:public QDialog{ Q_OBJECT public: Server(QWidget *parent=0); QPushButton *send; QUdpSocket *udpSocket; QTextEdit *textEdit; QVBoxLayout *layout; int port; private slots: void submit(); }; #endif // UPDSOCKET_H

 

 

实现代码

 

main.cpp

 

#include <QApplication> #include <QUdpSocket> #include <QTextEdit> #include <QVBoxLayout> #include <QPushButton> #include <QMessageBox> #include <QTextCodec> #include "updSocket.h" Server::Server(QWidget *parent):QDialog(parent){ port=4444; //创建 QUdpSocket 对象 udpSocket=new QUdpSocket(this); textEdit=new QTextEdit(); send=new QPushButton(tr("发送")); layout=new QVBoxLayout; layout->addWidget(textEdit); layout->addWidget(send); setLayout(layout); //关联 发送 信号到槽 connect(send,SIGNAL(clicked()),this,SLOT(submit())); setWindowTitle(tr("服务器端")); } void Server::submit(){ QMessageBox box; QString text=textEdit->toPlainText(); if(text.length()==0){ box.setText(tr("请输入发送内容")); } /*开始发送数据*/ //初始化长度 int len=0; //udpSocket->writeDatagram(发送的数据,发送数据的长度,IP,端口); 返回一个长度. len=udpSocket->writeDatagram(text.toLatin1(),text.length(),QHostAddress::LocalHost,port); if(len){ box.setText(tr("发送成功")); } box.exec(); } int main(int argc,char ** argv){ QApplication app(argc,argv); QTextCodec ::setCodecForTr(QTextCodec::codecForName("gb2312")); Server server; server.show(); return app.exec(); }

 

---------------下面是接收端也就是客户端----------------------------------

头文件

Client.h

#ifndef SERVER_H #define SERVER_H #include <QDialog> class QUdpSocket; class QPushButton; class QVBoxLayout; class QTextEdit; class Client :public QDialog{ Q_OBJECT public: Client(QWidget *parent=0); QPushButton *close; QUdpSocket *udpSocket; QVBoxLayout *layout; QTextEdit *textEdit; int port; private slots: void clientMessage(); }; #endif // SERVER_H

 

实现代码

 

main.cpp

 

#include <QApplication> #include <QPushButton> #include <QVBoxLayout> #include <QUdpSocket> #include <QTextEdit> #include <QTextCodec> #include <QMessageBox> #include "Client.h" Client::Client(QWidget *parent):QDialog(parent){ port=4444; close=new QPushButton(this); textEdit=new QTextEdit(this); close->setText(tr("关闭")); layout=new QVBoxLayout(this); layout->addWidget(close); layout->addWidget(textEdit); setLayout(layout); setWindowTitle(tr("接收端")); //实例化QUdpSocket 对象... udpSocket=new QUdpSocket(this); //监听端口 bool conn=udpSocket->bind(port); //链接失败 if(!conn){ QMessageBox box; box.setText(tr("链接错误")); box.exec(); }else{ //把udpSocket的信号关联到槽 connect(udpSocket,SIGNAL(readyRead()),this,SLOT(clientMessage())); } } void Client::clientMessage(){ while(udpSocket->hasPendingDatagrams()){ //是否读到数据 QByteArray data; //udpSocket->pendingDatagramSize 获取报文长度 //data.resize 给 data 数组设置长度 data.resize(udpSocket->pendingDatagramSize()); //读入数据 udpSocket->readDatagram(data.data(),data.size()); //显示数据内容 QString str=data.data(); textEdit->insertPlainText(str+"/r/n"); } } int main(int argc,char ** argv){ QApplication app(argc,argv); QTextCodec::setCodecForTr(QTextCodec::codecForName("gb2312")); Client client; client.show(); return app.exec(); }

 

 

-------------------------下面是 演示图片-------------------

[QT]QT教程之实例分析[九] 网络广播程序(QUdpSocket)_第1张图片

 

你可能感兴趣的:(网络,server,服务器,layout,qt,Signal)