多线程的QTcpServer
来源:互联网 发布:dhcp 服务器端口 编辑:程序博客网 时间:2024/06/16 13:30
来自
http://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
原始代码不再引用,只谈一个有意思的事情。
在多个socket连接中,可以不停的读或者写入,直到socket被断掉。
服务器代码
// myserver.h#ifndef MYSERVER_H#define MYSERVER_H#include <QTcpServer>class MyServer : public QTcpServer{ Q_OBJECTpublic: explicit MyServer(QObject *parent = 0); void startServer();signals: public slots: protected: void incomingConnection(qintptr socketDescriptor); };#endif // MYSERVER_H
// myserver.cpp#include "myserver.h"#include "mythread.h"MyServer::MyServer(QObject *parent) : QTcpServer(parent){}void MyServer::startServer(){ int port = 1234; if(!this->listen(QHostAddress::Any, port)) { qDebug() << "Could not start server"; } else { qDebug() << "Listening to port " << port << "..."; }}// This function is called by QTcpServer when a new connection is available. void MyServer::incomingConnection(qintptr socketDescriptor){ // We have a new connection qDebug() << socketDescriptor << " Connecting..."; // Every new connection will be run in a newly created thread MyThread *thread = new MyThread(socketDescriptor, this); // connect signal/slot // once a thread is not needed, it will be beleted later connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start();}
原始客户端socket连接进入后的处理
// mythread.cpp#include "mythread.h"MyThread::MyThread(qintptr ID, QObject *parent) : QThread(parent){ this->socketDescriptor = ID;}void MyThread::run(){ // thread starts here qDebug() << " Thread started"; socket = new QTcpSocket(); // set the ID if(!socket->setSocketDescriptor(this->socketDescriptor)) { // something's wrong, we just emit a signal emit error(socket->error()); return; } // connect socket and signal // note - Qt::DirectConnection is used because it's multithreaded // This makes the slot to be invoked immediately, when the signal is emitted. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); // We'll have multiple clients, we want to know which is which qDebug() << socketDescriptor << " Client connected"; // make this thread a loop, // thread will stay alive so that signal/slot to function properly // not dropped out in the middle when thread dies exec();}void MyThread::readyRead(){ // get the information QByteArray Data = socket->readAll(); // will write on server side window qDebug() << socketDescriptor << " Data in: " << Data; socket->write(Data);}void MyThread::disconnected(){ qDebug() << socketDescriptor << " Disconnected"; socket->deleteLater(); exit(0);}
对mythread.cpp主要做了两部分修改
1 将这部分使用单独DataClient类,每来一个连接(incommingconnections)
则DataClient * client = new DataClient();
QThread *pt = new QThread();
client->movetoThread(pt);
2 此种情况下则无法在thread中保证exec()循环。在readRead中执行循环。
循环语句类似
while(1) { if(socket->state() == QAbstractSocket::UnconnectedState|| socket->waitForDisconnected(1000)) { break; } qDebug() << "I am client " << socketDescriptor << ", i am working";}
0 0
- 多线程的QTcpServer
- QTcpServer多线程实现
- QTcpServer多线程实现
- QTcpServer多线程实现
- QTcpServer多线程实现
- 新手易步入的Qt中QTcpServer多线程误区
- 一个非常有趣的QTcpServer多线程编程问题
- QTcpServer使用多线程处理连接进来的QTcpSocket
- Qt中QTcpServer多线程误区
- Qt QTcpServer的使用
- QTcpServer的listen函数
- QTcpServer
- QTcpSocket QTcpServer 类的使用方法
- QTcpServer和QTcpSocket的使用
- QTcpServer如何监听两个不同的端口
- Qt网络编程QTcpServer和QTcpSocket的理解
- QTcpServer、QTcpSocket、QUdpSocket在聊天程序上的应用
- Qt网络编程QTcpServer和QTcpSocket的理解
- 撸代码--类QQ聊天实现(基于linux 管道 信号 共享内存)
- Android基础入门教程——1.6 .9(九妹)图片怎么玩
- ubuntu虚拟机安装 VMware-tools后在hgfs共享目录下查看不到共享文件
- 移动端尺寸基础知识
- 删除/usr/bin/vlc后,安装不上vlc问题解决
- 多线程的QTcpServer
- Android基础入门教程——1.7 界面原型设计
- 动态加载类
- 【网络流】HDU 3599 War 最多不相交路径
- PHP邮件发送类(SMTP)
- Eclipse中安装Genymotion插件解决方案
- 重写<小于号
- Android RecyclerView
- 使用XIB的UITableViewCell自适应,以及出现的问题进行解决