qt学习之路六(QT的tcp传输)
来源:互联网 发布:泡泡是什么聊天软件 编辑:程序博客网 时间:2024/05/06 02:41
这几天一直在做银行的管理系统,包括职工的登陆注册以及客户的开户转账存取款等功能,这几天天天通宵也写的差不多了,还剩下最后一个功能,交易记录的管理,差不多明天应该就能结束了。
这两天为了做这个东西,我把qt的网络模块初学了一下,学了一下qt的tcp传输,实现了服务器与客户端的通信。但是,在做的时候,我本来是打算做一个循环并发的服务器,但测试了一下后,突然发现,这个服务器根本不用开线程,它一直在工作,在一个客户端连接后,它就通过信号与槽函数,直接去处理,处理完后再返回,这样做,在连接数少的时候,确实和并发服务器差不多,但是做一个假设,假设有1000多台客户端连接的话,瞬间崩溃了,但我也不懂在哪边开线程合适,Linux中的话服务器在连接到后就应该开辟线程,但是qt中服务器在得到连接后直接去处理槽函数了,所以想了很久,还是不懂在哪边开辟线程,最终还是没有做出来,不过几十台的话处理起来应该没有什么问题(安慰自己。。)。
下面的代码(没有开线程):
/********************************************************************************** Form generated from reading UI file 'client.ui'**** Created: Thu Feb 16 16:09:39 2017** by: Qt User Interface Compiler version 4.8.1**** WARNING! All changes made in this file will be lost when recompiling UI file!********************************************************************************/#ifndef UI_CLIENT_H#define UI_CLIENT_H#include <QtCore/QVariant>#include <QtGui/QAction>#include <QtGui/QApplication>#include <QtGui/QButtonGroup>#include <QtGui/QDialog>#include <QtGui/QHeaderView>#include <QtGui/QLabel>#include <QtGui/QLineEdit>#include <QtGui/QPushButton>QT_BEGIN_NAMESPACEclass Ui_client{public: QLabel *label; QLabel *label_2; QLabel *messagelabel; QLineEdit *hostlineEdit; QLineEdit *portlineEdit; QPushButton *connect; void setupUi(QDialog *client) { if (client->objectName().isEmpty()) client->setObjectName(QString::fromUtf8("client")); client->resize(400, 300); label = new QLabel(client); label->setObjectName(QString::fromUtf8("label")); label->setGeometry(QRect(60, 70, 71, 31)); label_2 = new QLabel(client); label_2->setObjectName(QString::fromUtf8("label_2")); label_2->setGeometry(QRect(60, 130, 71, 31)); messagelabel = new QLabel(client); messagelabel->setObjectName(QString::fromUtf8("messagelabel")); messagelabel->setGeometry(QRect(80, 190, 171, 61)); hostlineEdit = new QLineEdit(client); hostlineEdit->setObjectName(QString::fromUtf8("hostlineEdit")); hostlineEdit->setGeometry(QRect(130, 80, 113, 20)); portlineEdit = new QLineEdit(client); portlineEdit->setObjectName(QString::fromUtf8("portlineEdit")); portlineEdit->setGeometry(QRect(130, 140, 113, 20)); connect = new QPushButton(client); connect->setObjectName(QString::fromUtf8("connect")); connect->setGeometry(QRect(260, 240, 75, 23)); retranslateUi(client); QMetaObject::connectSlotsByName(client); } // setupUi void retranslateUi(QDialog *client) { client->setWindowTitle(QApplication::translate("client", "client", 0, QApplication::UnicodeUTF8)); label->setText(QApplication::translate("client", "\344\270\273\346\234\272", 0, QApplication::UnicodeUTF8)); label_2->setText(QApplication::translate("client", "\347\253\257\345\217\243", 0, QApplication::UnicodeUTF8)); messagelabel->setText(QApplication::translate("client", "\346\216\245\346\224\266\345\210\260\347\232\204\344\277\241\346\201\257", 0, QApplication::UnicodeUTF8)); connect->setText(QApplication::translate("client", "\350\277\236\346\216\245", 0, QApplication::UnicodeUTF8)); } // retranslateUi};namespace Ui { class client: public Ui_client {};} // namespace UiQT_END_NAMESPACE#endif // UI_CLIENT_H
#ifndef CLIENT_H#define CLIENT_H#include <QDialog>#include <QAbstractSocket>#include <QTcpSocket>#include <QString>namespace Ui {class client;}class client : public QDialog{ Q_OBJECT public: explicit client(QWidget *parent = 0); ~client(); private: Ui::client *ui; QTcpSocket *tcpsocket; QString message; quint16 blocksize;private slots: void newconnect(); void readmessage(); void displayreeoe(QAbstractSocket::SocketError); void on_connect_clicked();};#endif // CLIENT_H
#include "client.h"#include "ui_client.h"#include <QtNetwork>client::client(QWidget *parent) : QDialog(parent), ui(new Ui::client){ ui->setupUi(this); tcpsocket = new QTcpSocket(this); connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(readmessage())); connect(tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayreeoe(QAbstractSocket::SocketError)));}client::~client(){ delete ui;}void client::newconnect(){ blocksize = 0; tcpsocket->abort(); tcpsocket->connectToHost(ui->hostlineEdit->text(), ui->portlineEdit->text().toInt());}void client::readmessage(){ QDataStream in(tcpsocket); in.setVersion(QDataStream::Qt_4_6); if(blocksize == 0) { if(tcpsocket->bytesAvailable() < (int)sizeof(quint16)) { return; } in>>blocksize; } if(tcpsocket->bytesAvailable() < blocksize) { return; } in>>message; ui->messagelabel->setText(message);}void client::displayreeoe(QAbstractSocket::SocketError){ qDebug()<<tcpsocket->errorString();}void client::on_connect_clicked(){ newconnect();}
#include <QtGui/QApplication>#include "client.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); client w; w.show(); return a.exec();}
/********************************************************************************** Form generated from reading UI file 'server.ui'**** Created: Thu Feb 16 15:41:59 2017** by: Qt User Interface Compiler version 4.8.1**** WARNING! All changes made in this file will be lost when recompiling UI file!********************************************************************************/#ifndef UI_SERVER_H#define UI_SERVER_H#include <QtCore/QVariant>#include <QtGui/QAction>#include <QtGui/QApplication>#include <QtGui/QButtonGroup>#include <QtGui/QDialog>#include <QtGui/QHeaderView>#include <QtGui/QLabel>QT_BEGIN_NAMESPACEclass Ui_server{public: QLabel *label; void setupUi(QDialog *server) { if (server->objectName().isEmpty()) server->setObjectName(QString::fromUtf8("server")); server->resize(400, 300); label = new QLabel(server); label->setObjectName(QString::fromUtf8("label")); label->setGeometry(QRect(70, 40, 121, 61)); retranslateUi(server); QMetaObject::connectSlotsByName(server); } // setupUi void retranslateUi(QDialog *server) { server->setWindowTitle(QApplication::translate("server", "server", 0, QApplication::UnicodeUTF8)); label->setText(QApplication::translate("server", "\347\255\211\345\276\205\350\277\236\346\216\245....", 0, QApplication::UnicodeUTF8)); } // retranslateUi};namespace Ui { class server: public Ui_server {};} // namespace UiQT_END_NAMESPACE#endif // UI_SERVER_H
#ifndef SERVER_H#define SERVER_H#include <QDialog>#include <QTcpServer>namespace Ui {class server;}class server : public QDialog{ Q_OBJECT public: explicit server(QWidget *parent = 0); ~server(); private: Ui::server *ui; QTcpServer *tcp_server;public slots: void sendmassage();};#endif // SERVER_H
#include "server.h"#include "ui_server.h"#include <QtNetwork>server::server(QWidget *parent) : QDialog(parent), ui(new Ui::server){ ui->setupUi(this); tcp_server = new QTcpServer(this); if(! tcp_server->listen(QHostAddress::LocalHost, 6666)) { qDebug()<<tcp_server->errorString(); close(); } connect(tcp_server, SIGNAL(newConnection()), this, SLOT(sendmassage()));}server::~server(){ delete ui;}void server::sendmassage(){ QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out<<(quint16)0; out<<tr("hello tcp!!!"); out.device()->seek(0); out<<(quint16)(block.size()-sizeof(quint16)); QTcpSocket *cfd = tcp_server->nextPendingConnection(); connect(cfd, SIGNAL(disconnected()), cfd, SLOT(deleteLater())); cfd->write(block); cfd->disconnectFromHost(); ui->label->setText("send message successful!");}
#include <QtGui/QApplication>#include "server.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); server w; w.show(); return a.exec();}
0 0
- qt学习之路六(QT的tcp传输)
- Qt学习之路_5(Qt TCP的初步使用)
- Qt学习之路_5(Qt TCP的初步使用)
- Qt学习之路_5(Qt TCP的初步使用)
- Qt学习之路_5(Qt TCP的初步使用)
- Qt学习之路_5(Qt TCP的初步使用)
- Qt学习之路_5(Qt TCP的初步使用)
- QT开发之TCP(传输文件)
- Qt之TCP、UDP传输模型
- Qt学习之六: Qt的对象模型
- Qt Tcp传输
- qt TCP传输字符串
- QT学习(六)QT之贪吃蛇
- QT--TCP ----传输图片(buf型)
- qt学习之路七(qt的数据库操作)
- <Qt>实例:TCP网络传输
- qt之tcp(一)
- qt之tcp(二)
- JVM内存配置参数
- hdu 2044 +hdu 2041(斐波那契递推)
- Hibernate的级联操作
- 二叉树-常见简单算法题
- 表单验证 脚本
- qt学习之路六(QT的tcp传输)
- 改变元素的css样式 脚本
- Eigen Face
- 《微服务》九大特性笔记
- Spring Mvc Upload -- 0配置文件
- Eclipse配置Resin应用服务器
- JSP基础
- 欢迎使用CSDN-markdown编辑器
- u725子集生成