由於本人沒有無線路由器,因此用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); //執行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
- #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;
- }
- }
- //讀取臨時文件中的標准輸出信息並寫入socket傳回給客戶端
- 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
- package com.tobacco.phonetest;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
-
- public class MainActivity extends Activity implements OnClickListener,Runnable{
-
- private Button button;
- private Button clear;
- private EditText editText;
- private TextView tv;
- private Socket socket;
- private PrintWriter pw;
- private BufferedReader br;
- //private InputStream is;
- private Handler handler;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- handler=new Handler();
- button=(Button)findViewById(R.id.button);
- clear=(Button)findViewById(R.id.clear);
- button.setOnClickListener(this);
- clear.setOnClickListener(this);
- editText=(EditText)findViewById(R.id.edittext);
- tv=(TextView)findViewById(R.id.textView);
- try {
- //socket=new Socket("tobacco5648.xicp.net",5648); //我的pc域名
- socket=new Socket("192.168.137.1",5648); //建立socket,這裡應該根據pc的ip地址進行修改
- //socket=new Socket("www.baidu.com",80);
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Log.e("socket","unknown host");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Log.e("socket","io execption");
- }
- if(socket==null){
- Log.e("socket","null");
- }
- else
- try {
- pw=new PrintWriter(socket.getOutputStream());
- br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"gbk"));
- String remote=socket.getInetAddress().getHostAddress();
- String self=socket.getLocalAddress().getHostAddress();
- tv.setText("connected!"+"\r\n"+"remote:"+remote+"\r\n"+"local:"+self+"\r\n");
- //is=socket.getInputStream();
- if(pw!=null&&br!=null){
- new Thread(this).start();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
-
- public void onClick(View view) {
- if(view==button){
- String str;
- str=editText.getText().toString();
- pw.print(str);
- pw.flush();
- }
- else if(clear==view){
- tv.setText("");
- }
-
- }
-
- public void run() {
- while(true){
- try {
- String str;
- while((str=br.readLine())!=null){
- final String s=str;
- handler.post(new Runnable(){
-
- public void run() {
- tv.setText(tv.getText()+s+"\r\n");
-
- }});
-
-
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }
-
-
- }