k81111:编程实现Android远程控制PC

因为本人没有无线路由器,是以用Win7自带的虚拟wifi让手机上网:然则,电脑不在我的房间,是以天天晚上在床上玩完手机还要下床去关电脑,让很懒很懒的本人很不爽,是以自己考试测验着做了一个远程节制。软件客户端运行在Android设备上(我的手机是An


当前位置: 主页 >k81111


因为本人没有无线路由器,是以用Win7自带的虚拟wifi让手机上网:

然则,电脑不在我的房间,是以天天晚上在床上玩完手机还要下床去关电脑,让很懒很懒的本人很不爽,是以自己考试测验着做了一个远程节制。

软件客户端运行在Android设备上(我的手机是Android的),办事器端运行在我的Win7本本上。客户端用的是Android平台利用编程,办事器端用的是Qt编写的。

首先设置好默认端口,然后设置节制措施,为了方便起见,这里直接使用标准C中的system接口(调用shell,Win7下便是cmd敕令提示符了)。客户端发送敕令给办事器,办事器直接履行敕令,并将标准输出返回给客户端。

下面看一些代码,详细代码可以到这里下载:

办事器端:

头文件:

mainwindow.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include

#include

#include

#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

#include

class MainWindow;

class Server:public QTcpServer

{

Q_OBJECT

private:

ik81111nt 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

#ik81111nclude

#include

#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

#include "mainwindow.h"

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

MainWindow w;

w.show();

return a.exec();

}

mainwindow.cpp

#include

#include

#include

#include

#include

#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

#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(reak81111dyRead()),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.Editk81111Text;

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("tobacco5648k81111.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();

}

}

}

}

发表评论
加载中...

相关文章