connect(this->tcpSocket,SIGNAL(readyRead()),this,SLOT(receiveData()));.....................................................//ЭТО НУЖНО В ОТДЕЛЬНЫЙ ПОТОК!void Session::receiveData(){ while(tcpSocket->bytesAvailable()) { QByteArray data=tcpSocket->readAll(); packetSplitter.setByteArray(&data,data.length()); }}
void Session::setClienSocket(QTcpSocket &socket){ this->tcpSocket=&socket; QThread* thread = new QThread; this->tcpSocket->moveToThread(thread); connect(thread, SIGNAL(started()), this, SLOT(startSocket())); thread->start(QThread::HighestPriority);}void Session::startSocket(){ packetSplitter.setEncryptor(packetEncryptor); connect(&packetSplitter,SIGNAL(onPacket(QByteArray*)),this,SLOT(onPacket(QByteArray*))); connect(this->tcpSocket,SIGNAL(disconnected()),this,SIGNAL(disconnect())); connect(this->tcpSocket,SIGNAL(readyRead()),this,SLOT(receiveData()));}
void Session::sendData(const QByteArray &packet){.............................................................. tcpSocket->write(data);}
QThread *thread = new QThread;Worker *worker = new Worker;worker->moveToThread(thread);thread->start();
C++ (Qt)#ifndef TCPSOCKET_H#define TCPSOCKET_H #include <QThread>#include <QtNetwork> typedef unsigned char BYTE; class TCPSocket : public QThread{ Q_OBJECTpublic: TCPSocket(QObject *parent = 0); void run(); void connectTo(QString hostName, int port); void sendData(const QByteArray &packet);private: QTcpSocket *socket; QString hostName; int port;signals: void connected(); void disconnected(); void notConnected();public slots: }; #endif // TCPSOCKET_H
C++ (Qt)#include "tcpsocket.h" TCPSocket::TCPSocket(QObject *parent) :QThread(parent){} void TCPSocket::connectTo(QString hostName, int port){ this->hostName=hostName; this->port=port; if(!isRunning()) start();} void TCPSocket::sendData(const QByteArray &packet){ socket->write(packet);} void TCPSocket::run(){ QString serverName = hostName; int serverPort = port; socket = new QTcpSocket(); socket->connectToHost(serverName, serverPort); if (!socket->waitForConnected(5000)) { emit notConnected(); return; } emit connected(); while(socket->waitForReadyRead(60000)){ while(socket->bytesAvailable()) { QByteArray data=socket->readAll(); qDebug()<<"Данные"; } } emit disconnected();}
C++ (Qt)void TCPSocket::sendData(const QByteArray &packet){ socket->write(packet,packet.length()); socket->flush();}
C++ (Qt)#ifndef TCPSOCKET_H#define TCPSOCKET_H #include <QThread>#include <QtNetwork> typedef unsigned char BYTE; class TCPSocket : public QThread{ Q_OBJECTpublic: TCPSocket(QObject *parent = 0); void run(); bool connectTo(QString hostName, int port); void sendData(const QByteArray &packet); void killConnection();private: QTcpSocket *socket;signals: void disconnected(); void error(QString error);}; #endif // TCPSOCKET_H
C++ (Qt)#include "tcpsocket.h" TCPSocket::TCPSocket(QObject *parent) :QThread(parent){} bool TCPSocket::connectTo(QString hostName, int port){ socket = new QTcpSocket(); connect(socket,SIGNAL(disconnected()),this,SIGNAL(disconnected())); socket->connectToHost(hostName, port); if (!socket->waitForConnected(5000)) { emit error(socket->errorString()); return false; }else{ if(!isRunning()) start(); return true; }} void TCPSocket::sendData(const QByteArray &packet){ socket->write(packet,packet.length()); socket->flush();} void TCPSocket::killConnection(){ socket->disconnectFromHost();} void TCPSocket::run(){ QTcpSocket socketReceiver; connect(&socketReceiver,SIGNAL(disconnected()),this,SIGNAL(disconnected())); socketReceiver.setSocketDescriptor(socket->socketDescriptor()); while(socketReceiver.waitForReadyRead(5000)){ while(socketReceiver.bytesAvailable()) { QByteArray data=socketReceiver.readAll(); qDebug()<<"Данные"; } } emit error(socketReceiver.errorString());}