[QT]QT教程之实例分析[九] 网络广播程序(QUdpSocket)
2010-06-13 11:17
691 查看
重点知识已近在代码里注释...
请仔细看代码
本文原创
转载请保留此链接 http://blog.csdn.net/siren0203
本章内容 以一个完整的 广播程序为实例 再次详细说明 UDP协议的使用方法..
发送端...
头文件
udpSocket.h
实现代码
main.cpp
---------------下面是接收端也就是客户端----------------------------------
头文件
Client.h
实现代码
main.cpp
-------------------------下面是 演示图片-------------------
请仔细看代码
本文原创
转载请保留此链接 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)
- 【PyQt4 实例19】基于UDP的网络广播程序
- Qt---基于UDP的网络广播程序
- Qt之QUdpSocket定时器网络广播
- 5.关于QT中的网络编程,QTcpSocket,QUdpSocket
- [QT]QT教程之实例分析[四]多窗口文档[QWorkspace]
- 在C++ Builder中用socket api来写网络通讯程序(同时支持TCP和UDP协议)
- 在C++ Builder中用socket api来写网络通讯程序(同时支持TCP和UDP协议)
- java学习之路——基于UDP的Socket网络通信实例
- qt5学习之网络通讯(qt5 udp 客户端和服务端程序)
- 网络编程-UDP程序实例(基础)
- 一个基于UDP的网络通迅程序(SOCKET)
- QT 通过UDP广播获取网络中所有设备信息
- java 网络编程【6】 用UDP编写网络聊天程序 以及 TCP编程实例
- 基于UDP协议的广播程序实例
- Linux内核分析 - 网络[十二]:UDP模块 - socket
- 网络编程与多线程的应用--基于socket udp编写一个简单聊天程序
- python3编写C/S网络程序实例教程
- Qt之UDP的网络广播例子
- linux网络编程之socket(十四):基于UDP协议的网络程序