5.关于QT中的网络编程,QTcpSocket,QUdpSocket
1
新建一个项目:TCPServer.pro
A
修改TCPServer.pro,注意:如果是想使用网络库,需要加上network
SOURCES TcpServer.cpp TcpClient.cpp HEADERS TcpServer.h TcpClient.h QT CONFIG |
B
新建如下文件,因为要用到网络库,所以加上network
C
编写IP选择下拉选,头文件ChooseInterface.h
#ifndef #define #include #include class { Q_OBJECT public: explicit QComboBox* QString signals: public void }; #endif |
编写ChooseInterface.cpp
#include #include #include ChooseInterface::ChooseInterface(QWidget QDialog(parent) { /* QList<QHostAddress> #if QList<QNetworkInterface> QList<QNetworkAddressEntry> entryList).broadcast() #endif //编写一个下拉选 _comboBox QVBoxLayout* lay->addWidget(_comboBox); foreach(QHostAddress { //将地址都转换成为ipv4的地址 quint32 //的ip if(ipaddr continue; //在comboBox中添加下拉选 _comboBox->addItem(QHostAddress(ipaddr).toString()); } //当下拉选发生变化时的操作 connect(_comboBox, this, } void { this->_strSelect } |
上面的运行结果是:
编写TcpServer.h |
#ifndef #define #include #include #include #include class { Q_OBJECT public: explicit QTcpServer* QTcpSocket* QTextBrowser* signals: public void void }; #endif |
编写TcpServer.cpp |
#include #include #include #include #include TcpServer::TcpServer(QWidget QWidget(parent) { // _server ChooseInterface dlg.exec(); //消息提示框 QMessageBox::information(NULL,"you _server->listen(QHostAddress(dlg._strSelect), // connect(_server, this, _show QHBoxLayout* lay->addWidget(_show); } void { // while(_server->hasPendingConnections()) { // _socket _show->append("New // connect(_socket, this, } } void { // while(_socket->bytesAvailable() { _show->append("Data QByteArray _show->append(buf); } } |
编写TcpClient.h |
#ifndef #define #include #include #include class { Q_OBJECT public: explicit QTcpSocket* QLineEdit* signals: public void }; #endif |
编写TcpClient.cpp |
#include #include #include TcpClient::TcpClient(QWidget QWidget(parent) { _socket _socket->connectToHost("127.0.0.1", _lineEdit QHBoxLayout* lay->addWidget(_lineEdit); QPushButton* lay->addWidget(button); connect(button, connect(_lineEdit, } void { QString if(strText.isEmpty()) return; _socket->write(strText.toUtf8()); _lineEdit->clear(); } |
MyWidget.h |
#ifndef #define #include class { Q_OBJECT public: explicit signals: public }; #endif |
MyWidget.cpp |
#include #include #include #include MyWidget::MyWidget(QWidget QWidget(parent) { } int { QApplication TcpServer TcpClient s.setWindowTitle("server"); c.setWindowTitle("client"); return } |
运行结果:
2
编写UDP程序
UDPServer.pro |
QT CONFIG HEADERS Udp1.h Udp2.h MyWidget.h SOURCES Udp1.cpp Udp2.cpp MyWidget.cpp |
Udp1.h |
#ifndef #define #include #include class { Q_OBJECT public: explicit QUdpSocket* signals: public void }; #endif |
Udp1.cpp |
#include #include #include Udp1::Udp1(QWidget QWidget(parent) { // _udp _udp->bind(10001); connect(_udp, this, // QTimer* timer->setInterval(1000); timer->start(); connect(timer, quint64 QString #if // _udp->writeDatagram(str.toUtf8(), #else // // // _udp->writeDatagram(str.toUtf8(), #endif }); } void { while(_udp->hasPendingDatagrams()) { quint32 QByteArray _udp->readDatagram(buf.data(), qDebug() } } |
Udp2.h |
#ifndef #define #include #include class { Q_OBJECT public: explicit QUdpSocket* signals: public void }; #endif |
Udp2.cpp |
#include #include #include #include Udp2::Udp2(QWidget QWidget(parent) { _udp // _udp->bind(QHostAddress::AnyIPv4, // _udp->joinMulticastGroup(QHostAddress("224.0.0.131")); connect(_udp, this, QTimer* timer->setInterval(1000); timer->start(); connect(timer, quint64 QString _udp->writeDatagram(str.toUtf8(), }); } void { while(_udp->hasPendingDatagrams()) { quint32 QByteArray _udp->readDatagram(buf.data(), qDebug() } } |
运行结果: 控制台输出结果如下: |
5.关于QT中的网络编程,QTcpSocket,QUdpSocket的更多相关文章
- Qt 多线程和网络编程学习
一,Qt多线程类学习 QThread类,开始一个新的线程就是开始执行重新实现QThread::run(),run()是默认现实调用exec(),QThread::start()开始线程的执行,run( ...
- QT中的SOCKET编程(QT-2.3.2)
转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...
- Qt学习之网络编程(一)
一些说明 学了有一段时间的python了,小项目做了不少,最近由于项目需要,所以要回归老本行了,开始重点突击C++和qt.python的网络爬虫系列有时间就更吧. 获取本机网络信息 在网络应用中,经常 ...
- QT中的SOCKET编程
转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...
- 网游中的网络编程系列1:UDP vs. TCP
原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 目录 网游中的网络编程系列1:UDP vs. TCP 网游中的网络编程2:发送和接收数据包 网游中的网 ...
- 网游中的网络编程3:在UDP上建立虚拟连接
目录 网游中的网络编程系列1:UDP vs. TCP 网游中的网络编程2:发送和接收数据包 网游中的网络编程3:在UDP上建立虚拟连接 TODO 二.在UDP上建立虚拟连接 介绍 UDP是无连接的,一 ...
- 第84节:Java中的网络编程(中)
第84节:Java中的网络编程(中) 实现客户端和服务端的通信: 客户端需要的操作,创建socket,明确地址和端口,进行键盘录入,获取需要的数据,然后将录入的数据发送给服务端,为socket输出流, ...
- 第78节:Java中的网络编程(上)
第78节:Java中的网络编程(上) 前言 网络编程涉及ip,端口,协议,tcp和udp的了解,和对socket通信的网络细节. 网络编程 OSI开放系统互连 网络编程指IO加网络 TCP/IP模型: ...
- 第62节:探索Java中的网络编程技术
前言 感谢! 承蒙关照~ 探索Java中的网络编程技术 网络编程就是io技术和网络技术的结合,网络模型的定义,只要共用网络模型就可以两者连接.网络模型参考. 一座塔有七层,我们需要闯关. 第一层物理层 ...
随机推荐
- [Codeforces 864F]Cities Excursions
Description There are n cities in Berland. Some pairs of them are connected with m directed roads. O ...
- [USACO13OPEN]照片Photo
题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N < ...
- ●洛谷P3242 [HNOI2015]接水果
题链: https://www.luogu.org/problemnew/show/P3242 题解: 整体二分,扫描线+树状数组. 详细的题解:http://blog.csdn.net/thy_as ...
- SpringCloud学习之soa基础
一.soa简单介绍 1)面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.SOA是解决复杂业务模块,提高扩展性,维护性,可伸缩 ...
- .net4.0设计模式(一)使用Lazy的单例模式
延迟加载,亦称延迟实例化,延迟初始化等, 主要表达的思想是,把对象的创建将会延迟到使用时创建,而不是在对象实例化时创建对象,即用时才加载.这种方式有助于提高于应用程序的性能,避免浪费计算,节省内存的使 ...
- JVM基础
1.基础 JDK 将java文件编译成class文件 JRE 包含JVM JVM可以进行内存管理 利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JDK中的编译程序(j ...
- lgp20151222 java中如何将Object类型转换为int类型
if (object instanceof Integer) { Integer.parseInt(object.toString()); } 很简单是不是?我就想提醒下自己,java有个特殊词 ...
- 安装插件出现eclipse An internal error occurred during: "Installing Software". xxxxxxxxx
就是你自己本来就有那个插件了 百度怎么删吧.... 看一下我这个文章 强烈建议本地安装的时候用第四种安装 http://www.cnblogs.com/ydymz/articles/7203260.h ...
- vue mint-ui 实现省市区街道4级联动(仿淘宝京东收货地址4级联动)
demo及源码地址 https://github.com/artiely/citypicker 先去下载一个“省份.城市.区县.乡镇” 四级联动数据,然后 引入 import { Picker } f ...
- FastReport报表MVC显示步骤
FastReport报表MVC使用步骤如下: 1.创建MVC网站项目 最终DEMO如下图所示 2.引用相关DLL FastReport.dll FastReport.Web.dll 3.Web.con ...