QT之远程控制对方电脑

QT之远程控制对方电脑

2018-08-01 17:08:28 小生无名 阅读数 2414更多

分类专栏: QT控件插件大全

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/cqltbe131421/article/details/81334266

qq:609162385 
QT之远程控制对方电脑_第1张图片

#ifndef CONTROLER_H
#define CONTROLER_H

#include "interface.h"
#include "mapthread.h"
#include "cmdthread.h"

#include 
#include 
#include 
#include 
#include 
#include 

typedef struct SocketAndThread
{
    QTcpSocket* socket;
    QThread*  thread;
} SocketAndThread;

class Controler : public QObject
{
    Q_OBJECT
private:
    QTcpServer* pCmdServer;
    QTcpServer* pMapServer;

    QVector* vec;

public:
    const static int DATA_HEARER_MAP = 1;    //图像数据
    const static int DATA_HEADER_CMD = 2;    //命令数据
    const static int DATA_HEARER_RES = 3;    //命令结果数据

    const static int MAP_SERVER_PORT = 5648;
    const static int CMD_SERVER_PORT = 5649;

    explicit Controler(QObject *parent = 0);

public slots:
    void newMapClient();
    void newCmdClient();
    void someSocketDisconnected();
};

#endif // CONTROLER_H
#include "controler.h"

Controler::Controler(QObject *parent) :
    QObject(parent)
{
    vec = new QVector;
    pMapServer = new QTcpServer;
    pMapServer->listen(QHostAddress::Any, MAP_SERVER_PORT);
    qDebug()<<"map server listening"<listen(QHostAddress::Any, CMD_SERVER_PORT);
    qDebug()<<"cmd server listening"<nextPendingConnection();
    qDebug()<<"new map connection:"<peerAddress().toString()<append(st);

    mapThread->start();
}

void Controler::newCmdClient()
{
    QTcpSocket* clientCmdSocket = pCmdServer->nextPendingConnection();
    qDebug()<<"new cmd connection:"<peerAddress().toString()<append(st);

    cmdThread->start();
}

void Controler::someSocketDisconnected()
{
    for(int i = 0; i < vec->size(); i ++)
    {
        if(vec->at(i).socket->state() == QAbstractSocket::UnconnectedState)
        {
            if(vec->at(i).socket->isOpen())
                vec->at(i).socket->close();
            vec->at(i).thread->quit();
            connect(vec->at(i).thread, SIGNAL(finished()), vec->at(i).thread, SLOT(deleteLater()));
            vec->remove(i);
            i --;
        }
    }
}

你可能感兴趣的:(Qt)