由于本人没有无线路由器,因此用Win7自带的虚拟wifi让手机上网:Win7虚拟wifi
但是,电脑不在我的房间,因此每天晚上在床上玩完手机还要下床去关电脑,让很懒很懒的本人很不爽,因此自己尝试着做了一个远程控制。
软件客户端运行在Android设备上(我的手机是Android的),服务器端运行在我的Win7本本上。客户端用的是Android平台应用编程,服务器端用的是Qt编写的。
首先设置好默认端口,然后设置控制方法,为了方便起见,这里直接利用标准C中的system接口(调用shell,Win7下就是cmd命令提示符了)。客户端发送命令给服务器,服务器直接执行命令,并将标准输出返回给客户端。
下面看一些代码,具体代码可以到这里下载:工程源代码
服务器端:
头文件:
mainwindow.h
- #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
- #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
- #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
- #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
- #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);
- 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
- #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 printText(ch);
- ServerThread *thread=new ServerThread(handle,this,mainWindow,this->clientNum);
- this->clientNum++;
- thread->start();
- }
serverthread.cpp
- #include "serverthread.h"
- #include <QFile>
- #define COMMAND_SIZE 50
- ServerThread::ServerThread(int socketDescriptor,QObject *parent,MainWindow *m,int c):QThread(parent)
- {
- this->mainWindow=m;
- this->clientCode=c;
- socket=new QTcpSocket(parent);
- socket->setSocketDescriptor(socketDescriptor);
- }
-
- void ServerThread::run()
- {
- this->connect(socket,SIGNAL(readyRead()),this,SLOT(receiveData()));
- connect(this,SIGNAL(newCommand(const char*,int,const char*,int)),this->mainWindow,SLOT(execCommand(const char*,int,const char*,int)));
- connect(this->mainWindow,SIGNAL(writeFile(int)),this,SLOT(readFile(int)));
- exec();
- }
-
- void ServerThread::receiveData()
- {
- int left=socket->bytesAvailable();
- char *command=new char[COMMAND_SIZE];
- while(left>0)
- {
- int read=socket->readLine(command,COMMAND_SIZE);
- emit newCommand(command,read,"test",this->clientCode);
- left-=read;
- }
- }
- void ServerThread::readFile(int cC)
- {
- if(cC==this->clientCode)
- {
- QFile *file=new QFile("temp.txt");
- if(file->open(QIODevice::ReadWrite))
- {
- char *buffer=new char[100];
- int length;
- while((length=file->read(buffer,100))>0)
- {
- socket->write(buffer,length);
- }
- }
- socket->flush();
- file->close();
- system("del temp.txt");
- }
- }
客户端:
只有一个Acitvity
MainActivity.Java