C++ (Qt)#include "myserver.h" MyServer::MyServer(QObject *parent) : QTcpServer(parent){} void MyServer::StartServer(){ if(!this->listen(QHostAddress::Any,1234)) { qDebug() << "Could not start server"; thread = new MyThread(this); connect(thread, SIGNAL(finished()),thread, SLOT(deleteLater())); thread->start(); } else { qDebug() << "Listening..."; }} void MyServer::incomingConnection(int socketDescriptor){ qDebug() << socketDescriptor << " Connecting..."; thread->newConnection(socketDescriptor); }
C++ (Qt)#include "mythread.h" MyThread::MyThread( QObject *parent) : QThread(parent){ blockSize = 0;// socket = new QTcpSocket(this);} void MyThread::run(){ //thread starts here qDebug() << " Starting thread";// socket = new QTcpSocket();// if(!socket->setSocketDescriptor(this->socketDescriptor))// {// emit error(socket->error());// return;// } // connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);// connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);// sockets.append(socket);// qDebug() << socketDescriptor << " Client Connected"; exec();}void MyThread::newConnection(int sockDesc){ qDebug() << sockDesc << " Client Connected"; QTcpSocket *socket = new QTcpSocket(); if(!socket->setSocketDescriptor(sockDesc)) { emit error(socket->error()); return; } connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);// падает тут connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection); sockets.append(socket); } void MyThread::readyRead(){ QString message; for(int i=0; i<sockets.size(); i++) { while(sockets[i]->bytesAvailable()) { QDataStream in(sockets[i]); in.setVersion(QDataStream::Qt_4_7); if (blockSize == 0) { if (sockets[i]->bytesAvailable() < (int)sizeof(quint32)) return; in >> blockSize; } if (sockets[i]->bytesAvailable() < blockSize) return; in>>message; } blockSize=0; } text = message; qDebug() << socketDescriptor << " Data in: " << message; SendData();} void MyThread::disconnected(){ for (int i=0; i<sockets.size(); i++) if (sockets[i]->state()!=QAbstractSocket::ConnectedState) { qDebug() << sockets[i]->socketDescriptor() << "Disconnected"; sockets[i]->deleteLater(); sockets.removeAt(i); } if(sockets.empty()) exit(0);}void MyThread::SendData(){ QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_7); out << (quint32)0; out << text; out.device()->seek(0); out << (quint32)(block.size() - sizeof(quint32)); for(int i=0; i<sockets.size(); i++) sockets[i]->write(block);}
C++ (Qt)#include "myserver.h" MyServer::MyServer(QObject *parent) : QTcpServer(parent){ client = new QTcpSocket(); blockSize = 0;} void MyServer::StartServer(){ if(!this->listen(QHostAddress::Any,1234)) { qDebug() << "Could not start server"; } else { qDebug() << "Listening..."; }} void MyServer::incomingConnection(int socketDescriptor){ qDebug() << socketDescriptor << " Connecting..."; if(!client->setSocketDescriptor(socketDescriptor)) {// emit error(client->error()); return; } connect(client,SIGNAL(readyRead()),this,SLOT(ReceiveData()),Qt::DirectConnection); connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection); sockets.append(client); qDebug() << socketDescriptor << " Client Connected";}void MyServer::ReceiveData(){ QString message; for(int i=0; i<sockets.size(); i++) { while(sockets[i]->bytesAvailable()) { QDataStream in(sockets[i]); in.setVersion(QDataStream::Qt_4_7); if (blockSize == 0) { if (sockets[i]->bytesAvailable() < (int)sizeof(quint32)) return; in >> blockSize; } if (sockets[i]->bytesAvailable() < blockSize) return; in>>message; } blockSize=0; } text = message; qDebug() << " Data in: " << message; thread = new MyThread(sockets, text); connect(thread, SIGNAL(finished()),thread, SLOT(deleteLater())); thread->start();}void MyServer::disconnected(){ for (int i=0; i<sockets.size(); i++) if (sockets[i]->state()!=QAbstractSocket::ConnectedState) { qDebug() << sockets[i]->socketDescriptor() << "Disconnected"; sockets[i]->deleteLater(); sockets.removeAt(i); }}
C++ (Qt)#include "mythread.h" MyThread::MyThread(QList <QTcpSocket *> clients, QString mes, QObject *parent) : QThread(parent){ sockets = clients; text = mes;} void MyThread::run(){ //thread starts here qDebug() << " Starting thread"; if(sockets.isEmpty()) exit(0); QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_7); out << (quint32)0; out << text; out.device()->seek(0); out << (quint32)(block.size() - sizeof(quint32)); for(int i=0; i<sockets.size(); i++) sockets[i]->write(block); exec();}