Android实现远程控制PC(Android[客户端]+Qt[服务器端])
由于本人没有无线路由器,因此用Win7自带的虚拟wifi让手机上网:Win7虚拟wifi
但是,电脑不在我的房间,因此每天晚上在床上玩完手机还要下床去关电脑,让很懒很懒的本人很不爽,因此自己尝试着做了一个远程控制。
软件客户端运行在Android设备上(我的手机是Android的),服务器端运行在我的Win7本本上。客户端用的是Android平台应用编程,服务器端用的是Qt编写的。
首先设置好默认端口,然后设置控制方法,为了方便起见,这里直接利用标准C中的system接口(调用shell,Win7下就是cmd命令提示符了)。客户端发送命令给服务器,服务器直接执行命令,并将标准输出返回给客户端。
下面看一些代码,具体代码可以到这里下载:工程源代码
服务器端:
头文件:
mainwindow.h
[cpp]
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork/QTcpServer>
#include <QTextDocument>
#include "server.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
QTextDocument *textDocument;
Server *server;
~MainWindow();
public slots:
void getConnect();
void execCommand(const char *command,int length,const char *add,int clientCode);
void showText(const char *text);
signals:
void writeFile(int clientCode);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
server.h
[cpp]
#ifndef SERVER_H
#define SERVER_H
#include <QTextDocument>
#include <QTcpServer>
class MainWindow;
class Server:public QTcpServer
{
Q_OBJECT
private:
int port;
MainWindow *mainWindow;
QHostAddress hostAddress;
int clientNum; //已建立的客户端连接数
public:
Server(MainWindow *m,int p,QHostAddress a);
void incomingConnection(int handle);
signals:
void printText(const char *text);
};
#endif // SERVER_H
serverthread.h
[cpp]
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include "mainwindow.h"
class ServerThread:public QThread
{
Q_OBJECT
private:
QTcpSocket *socket;
MainWindow *mainWindow;
int clientCode;
public:
ServerThread(int socketDescriptor,QObject *parent,MainWindow *m,int clientCode);
void run();
signals:
void newCommand(const char *str,const int length,const char *add,int clientCode);
public slots:
void receiveData();
void readFile(int cC);
};
#endif // SERVERTHREAD_H
源代码:
main.cpp
[cpp]
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
[cpp]
#include <QtNetwork/QHostAddress>
#include <QTextDocument>
#include <string>
#include <cstring>
#include <string.h>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
server=new Server(this,5648,QHostAddress::Any); //初始化服务器
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::getConnect()
{
QTextDocument *textDocumentM=new QTextDocument(QString("new connect !"),this);
ui->textEdit->setDocument(textDocumentM);
server->nextPendingConnection();
}
void MainWindow::execCommand(const char *command,int length,const char *add, int clientCode)
{
QTextDocument *textDocumentC=new QTextDocument(QString("message"),this);
char *commandStr=new char[length+15];
strcpy(commandStr,command);
char *para=commandStr+length;
char *c=" > temp.txt"; //将结果重定向到一个临时文件
strcpy(para,c);
system(commandStr); //执行shell命令
emit writeFile(clientCode);
ui->textEdit->setDocument(textDocumentC);
}
void MainWindow::showText(const char *text)
{
QTextDocument *textDocumentT=new QTextDocument(QString(text),this);
ui->textEdit->setDocument(textDocumentT);
}
server.cpp
[cpp]
#include "server.h"
#include "serverthread.h"
Server::Server(MainWindow *m,int p,QHostAddress a)
{
this->hostAddress=a;
this->port=p;
this->mainWindow=m;
this->clientNum=0; //初始化客户连接数
this->QTcpServer::listen(hostAddress,quint16(port));
connect(this,SIGNAL(printText(const char*)),this->mainWindow,SLOT(showText(const char*)));
}
void Server::incomingConnection(int handle)
{
char ch[]="new connection !";
emit printT
补充:移动开发 , Android ,