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技术和网络技术的结合,网络模型的定义,只要共用网络模型就可以两者连接.网络模型参考. 一座塔有七层,我们需要闯关. 第一层物理层 ...
随机推荐
- [HNOI2004]树的计数
题目描述 输入输出格式 输入格式: 输入文件第一行是一个正整数n,表示树有n个结点.第二行有n个数,第i个数表示di,即树的第i个结点的度数.其中1<=n<=150,输入数据保证满足条件的 ...
- 【bzoj3173-最长上升子序列-一题两解】
这道题不就是简单的DP吗,BZOJ在水我!不,你是错的. ·本题特点: 不断向不同位置插入数字(按数字1,2,3,4,5,6……),需要求出每一次插入后的最长上升子序列. ·分析 ...
- bzoj3944Sum
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5149 Solved: 1385[Submit][Status][Discuss ...
- bzoj3786星系探索 splay
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1314 Solved: 425[Submit][Status][Discuss ...
- oracle 分页查询数据重复问题
最近在做项目的时候发现一个问题,oracle 在查询分页数据的时候,有几条数据重复查询了,并且有几条数据在分页的时候消失了.百度了一下发现,ORACLE 在查询数据的时候返回的行不是固定的,他只是按照 ...
- OCP 认证考试报名费技巧题库051052053解析合格线
本人于2017年4月22日通过参加OCP考试,第一次参加,一天之内考了三门,三门一次性通过,052 - 95% ,053 - 86% ,051 - 100% 一.关于考试考试报名费: 052:158$ ...
- jenkins更新后出现JNLP-connect,JNLP2-connect警告
在更新jenkins后出现提示 This Jenkins instance uses deprecated protocols: JNLP-connect,JNLP2-connect. It may ...
- java读取mysql表的注释及字段注释
/** * 读取mysql某数据库下表的注释信息 * * @author xxx */ public class MySQLTableComment { public static Connectio ...
- setTimeout、setInterval被遗忘的第三个参数
一.最近在看promise,惊奇的发现:原来 setTimeout不只有两个参数,我还能说什么呢?赶紧探探究竟. function multiply(input) { return new Promi ...
- ubuntu 卸载从源码安装的 emacs
由于配置问题想卸了重装. 解压并进入你的源码所在目录: ./configure sudo make uninstall Done Reference: http://askubuntu.com/que ...