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


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

    请下载 Qt 5.12.12

    server.pro :

    1. QT += gui network
    2. CONFIG += c++11 console
    3. CONFIG -= app_bundle
    4. # You can make your code fail to compile if it uses deprecated APIs.
    5. # In order to do so, uncomment the following line.
    6. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    7. SOURCES += \
    8. main.cpp \
    9. mythread.cpp \
    10. server.cpp
    11. # Default rules for deployment.
    12. qnx: target.path = /tmp/$${TARGET}/bin
    13. else: unix:!android: target.path = /opt/$${TARGET}/bin
    14. !isEmpty(target.path): INSTALLS += target
    15. HEADERS += \
    16. mythread.h \
    17. server.h

    mythread.h :

    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. class myThread : public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. myThread(qintptr ID,QObject *parent = 0);
    12. qintptr sockethandle;
    13. QTcpSocket *socket;
    14. void run();
    15. QProcess *process;
    16. public slots:
    17. void readyRead();
    18. void disconnected();
    19. void fun();
    20. };
    21. #endif // MYTHREAD_H

    server.h :

    1. #ifndef SERVER_H
    2. #define SERVER_H
    3. #include
    4. #include "mythread.h"
    5. class Server : public QTcpServer
    6. {
    7. public:
    8. Server();
    9. void startServer();
    10. protected:
    11. void incomingConnection(qintptr handle);
    12. };
    13. #endif // SERVER_H

    mythread.cpp :

    1. #include "mythread.h"
    2. myThread::myThread(qintptr ID,QObject *parent):QThread(parent)
    3. {
    4. sockethandle = ID;
    5. process = new QProcess(this);
    6. connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(fun()));
    7. }
    8. void myThread::run()
    9. {
    10. qDebug() << sockethandle;
    11. socket = new QTcpSocket(this);
    12. socket->setSocketDescriptor(sockethandle);
    13. connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    14. connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    15. exec();
    16. }
    17. void myThread::readyRead()
    18. {
    19. QByteArray data = socket->readAll();
    20. qDebug() << data;
    21. process->start("bash");
    22. process->write(data);
    23. }
    24. void myThread::disconnected()
    25. {
    26. socket->deleteLater();
    27. exit(0);
    28. }
    29. void myThread::fun()
    30. {
    31. socket->write(process->readAllStandardOutput().data());
    32. }

    server.cpp :

    1. #include "server.h"
    2. Server::Server()
    3. {
    4. }
    5. void Server::incomingConnection(qintptr handle)
    6. {
    7. myThread *thread = new myThread(handle);
    8. thread->start();
    9. }
    10. void Server::startServer()
    11. {
    12. if(this->listen(QHostAddress::Any,1984)){qDebug() << "Listen ...";}
    13. }

    main.cpp :

    1. #include
    2. #include "server.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QCoreApplication a(argc, argv);
    6. Server ser;
    7. ser.startServer();
    8. return a.exec();
    9. }

  • 相关阅读:
    09_分割等和子集
    Android App开发超实用实例 | 约束布局
    可视化 & Echarts
    纯手工总结超详细关于计算机网络的五层知识点,看看你都掌握了没
    MacOS Ventura 13 优化配置(ARM架构新手向导)
    MATLAB算法实战应用案例精讲-【推荐系统】CTR预估模型(补充篇)
    做运维有前途吗?
    光伏无人机吊装技术的优势及应用前景
    科技资讯|Vision Pro头显无损音频仅限USB-C AirPods Pro 2耳机
    Device Partner 平台合作伙伴认证和数据安全保护
  • 原文地址:https://blog.csdn.net/nn_84/article/details/136161023