多线程的QTcpServer
2015-07-20 09:03
609 查看
来自
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_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer();
signals:
public slots:
protected:
void incomingConnection(qintptr socketDescriptor);
};
#endif // MYSERVER_H
原始客户端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";
}
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_OBJECT
public:
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";
}
相关文章推荐
- Xcode7中关于下载网络图片报错的问题
- TCP/IP详解学习笔记(1)--基本概述
- TCP/IP 网络编程(六)
- C#中发送HttpPost请求来调用WebService
- 解决VM虚拟机连不上网络的问题
- 微软“小冰”识狗与人工神经网络(V)
- 1、linux网络服务实验 用PuTTY连接Linux
- [原]理解HTTP之Content-Type
- Apache Web Server -> httpd 的长连接配置
- JAVA基础_Java网络编程
- 高校网络视频直播流媒体应用
- hdu 5038 (2014北京网络赛G 排序水题)
- Java中HTTP通信
- HTTP协议(二)之HTTP请求
- HTTP 笔记与总结(8)HTTP 与内容压缩
- linux下的防火墙工具TCP Wrappers|Netfilter简介
- Volley和OkHttp并用时导致http header数据被覆盖的bug
- 网络寻路
- 关于网络开发中JSON的使用
- OSI七层网络模型与TCP/IP四层网络模型