Qt的Socket数据通讯的一个例子。
2016-06-11 00:00
525 查看
QTcpServer类用来侦听端口,获取QTcpSocket.
QTcpSocket有connected的信号(已经连接),还有readyread()信号,表示已经有数据发过来了,准备读取。
若要关闭当前连接只需要调用qtcpsocket::close();就关闭了当前连接
下面有两个例子
服务器端
用的是控制台程序(QT)当用户发送数据过来就cout显示,然后就write一个ILoveYou的字符串返回到客户端。然后close断开连接
客户端
用的书图形界面,一个输入框输入数据然后发送,最后QMessagebox显示服务器返回消息
=======================
服务器端(三个文件)
运行截图:
n
QTcpSocket有connected的信号(已经连接),还有readyread()信号,表示已经有数据发过来了,准备读取。
若要关闭当前连接只需要调用qtcpsocket::close();就关闭了当前连接
下面有两个例子
服务器端
用的是控制台程序(QT)当用户发送数据过来就cout显示,然后就write一个ILoveYou的字符串返回到客户端。然后close断开连接
客户端
用的书图形界面,一个输入框输入数据然后发送,最后QMessagebox显示服务器返回消息
=======================
服务器端(三个文件)
#ifndefMYSERVER_H #defineMYSERVER_H #include<QTcpSocket> #include<iostream> #include<QObject> #include<QTcpServer> classmyserver:publicQTcpServer { Q_OBJECT public: QTcpSocket*socket; QTcpServer*server; myserver(); privateslots: voidgetData(); voidnewconnectslot(); }; #endif//MYSERVER_H #include"myserver.h" #include<QByteArray> #include<QString> #include<QDataStream> myserver::myserver() { this->socket=newQTcpSocket; this->server=newQTcpServer; if(this->server->listen(QHostAddress::Any,520)) { std::cout<<"bindport520successful."<<std::endl; }else { std::cout<<"bindport520failed."<<std::endl; } QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newconnectslot())); } voidmyserver::newconnectslot() { this->socket=this->server->nextPendingConnection(); connect(this->socket,SIGNAL(readyRead()),this,SLOT(getData())); } voidmyserver::getData() { QByteArrayby=this->socket->readAll(); QDataStreamds(by); QStringx; ds>>x; QByteArrayba=x.toLatin1(); char*p=ba.data(); std::cout<<p<<std::endl; socket->write("Iloveyou");//返回给客户端 this->socket->close();//断开连接 } #include<QCoreApplication> #include<iostream> #include"myserver.h" #include<QHostAddress> intmain(intargc,char*argv[]) { QCoreApplicationa(argc,argv); std::cout<<"--ServerinitializedByHanHan--"<<std::endl; myserver*server=newmyserver; returna.exec(); }
客户端(三个文件)
#ifndefMAINWINDOW_H#defineMAINWINDOW_H#include<QMainWindow>#include<QString>#include<QByteArray>#include<QDataStream>#include<QTcpSocket>namespaceUi{classMainWindow;}classMainWindow:publicQMainWindow{Q_OBJECTpublic:QTcpSocket*socket;explicitMainWindow(QWidget*parent=0);~MainWindow();privateslots:voidconnnectslot();voidon_btn_send_clicked();voidreadyslot();private:Ui::MainWindow*ui;};#endif//MAINWINDOW_H
#include"mainwindow.h"#include"ui_mainwindow.h"#include<QHostAddress>#include<QMessageBox>MainWindow::MainWindow(QWidget*parent):&nbs
7fe0
p;QMainWindow(parent),ui(newUi::MainWindow){ui->setupUi(this);this->socket=newQTcpSocket;}MainWindow::~MainWindow(){deleteui;}voidMainWindow::on_btn_send_clicked(){QHostAddressaddress("127.0.0.1");this->socket->connectToHost(address,520);connect(this->socket,SIGNAL(connected()),this,SLOT(connnectslot()));connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyslot()));//接收发来的数据}voidMainWindow::connnectslot(){QStringdata=this->ui->data_edit->toPlainText();QByteArrayarray;QDataStreamds(&array,QIODevice::WriteOnly);ds<<data;this->socket->write(array);}
voidMainWindow::readyslot(){QStringx=this->socket->readAll();QMessageBox::about(this,"x",x);}#include"mainwindow.h"#include<QApplicatio>
intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
MainWindoww;
w.show();
returna.exec();
}
运行截图:
n
相关文章推荐
- Qt5.3企业版和开源版功能区别
- Qt在各平台上的搭建qt-everywhere
- qt 操作word
- Qt SQL Programming 部分翻译
- Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Qt 错误: 无法运行 release 下的可执行文件
- Qt 技巧:设置在 debug 路径下直接运行可执行文件
- Qt 技巧:去除对话框边框 + 设置窗口可移动和透明
- Qt布局与分割器QSplitter
- Qt中addStretch的有趣应用
- Qt Creator插件工作流程代码走读
- QT中的qmake详解
- Qt setStyleSheet 添加背景色/背景图片(取消背景色,读取本地文件作为背景色)
- QT实现窗口缩放打开与关闭(重叠窗口,太有意思了)
- QT动画介绍(有例子可以下载)
- QT通过IP地址定位地址(用get方法取数据)
- 亲测VS2010纯静态编译QT4.8.0,实现VS2010编译调试Qt程序,QtCreator静态发布程序(图文并茂,非常详细) good
- Qt configure 参数不完全说明
- QT设置前景图位置(配色简单漂亮)
- QT图片旋转