C# 实现远程控制软件的关键技术
一、服务器端多线程Socket技术 用TcpListener进行侦听,接受客户端连接,有客户端连进来后开启处理线程处理数据,代码如下: using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 在8888端口侦听
TcpListener serverSocket = );
TcpClient clientSocket = default(TcpClient);
;
serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");
counter = ;
while (true)
{
counter += ;
// 接受客户端连接
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
// 启动客户端处理代码
handleClinet client = new handleClinet();
client.startClient(clientSocket, Convert.ToString(counter));
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}
// 客户端连接处理类
public class handleClinet
{
TcpClient clientSocket;
string clNo;
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
// 开启处理线程
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
;
];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = ;
while ((true))
{
try
{
requestCount = requestCount + ;
// 读取内容
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, , (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);
rCount = Convert.ToString(requestCount);
serverResponse = "Server to clinet(" + clNo + ") " + rCount;
sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, , sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
}
}
}
}
}
二、鼠标控制技术 鼠标的控制用到了 mouse_event 这个API函数,参考代码如下: using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MouseControl
{
class MouseControl
{
/// <summary>
/// 鼠标控制参数
/// </summary>
const int MOUSEEVENTF_LEFTDOWN = 0x2;
const int MOUSEEVENTF_LEFTUP = 0x4;
const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
const int MOUSEEVENTF_MIDDLEUP = 0x40;
const int MOUSEEVENTF_MOVE = 0x1;
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
const int MOUSEEVENTF_RIGHTDOWN = 0x8;
const int MOUSEEVENTF_RIGHTUP = 0x10;
/// <summary>
/// 鼠标的位置
/// </summary>
public struct PONITAPI
{
public int x, y;
}
[DllImport("user32.dll")]
public static extern int GetCursorPos(ref PONITAPI p);
[DllImport("user32.dll")]
public static extern int SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[STAThread]
static void Main()
{
PONITAPI p = new PONITAPI();
GetCursorPos(ref p);
Console.WriteLine("鼠标现在的位置X:{0}, Y:{1}", p.x, p.y);
Console.WriteLine("Sleep 1 sec...");
Thread.Sleep();
p.x = (new Random()).Next(Screen.PrimaryScreen.Bounds.Width);
p.y = (new Random()).Next(Screen.PrimaryScreen.Bounds.Height);
Console.WriteLine("把鼠标移动到X:{0}, Y:{1}", p.x, p.y);
SetCursorPos(p.x, p.y);
GetCursorPos(ref p);
Console.WriteLine("鼠标现在的位置X:{0}, Y:{1}", p.x, p.y);
Console.WriteLine("Sleep 1 sec...");
Thread.Sleep();
Console.WriteLine("在X:{0}, Y:{1} 按下鼠标左键", p.x, p.y);
mouse_event(MOUSEEVENTF_LEFTDOWN, p.x, p.y, , );
Console.WriteLine("Sleep 1 sec...");
Thread.Sleep();
Console.WriteLine("在X:{0}, Y:{1} 释放鼠标左键", p.x, p.y);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, , );
Console.WriteLine("程序结束,按任意键退出....");
Console.ReadKey();
}
}
}
三、键盘控制技术 键盘的控制用到了 keybd_event 这个API函数,参考代码段如下: [DllImport("user32.dll", EntryPoint = "keybd_event")]
public static extern void keybd_event(
byte bVk,
byte bScan,
int dwFlags,
int dwExtraInfo
);
keybd_event((, , );//按下F11
keybd_event((, ); //弹起F11
四、运行程序4.1 public static void RunProcess(string name, string command)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = name;
myProcess.StartInfo.Arguments = command;
myProcess.Start();
return;
}
4.2 运行CMD并取得命令执行结果 public static string RunCmd(string command)//运行一个cmd命令
{
Process p = new Process();
//p.StartInfo.WorkingDirectory = "c:\\"; // 工作目录
p.StartInfo.FileName = "cmd.exe"; // 程序名
p.StartInfo.Arguments = "/c " + command; // 执行参数
p.StartInfo.UseShellExecute = false; // 关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; // 重定向标准输入
p.StartInfo.RedirectStandardOutput = true; // 重定向标准输出
p.StartInfo.RedirectStandardError = true; // 重定向错误输出
p.StartInfo.CreateNoWindow = true; // 设置不显示窗口
p.Start(); //启动
//p.StandardInput.WriteLine(command); // 也可以用这种方式输入要执行的命令
//p.StandardInput.WriteLine("exit"); // 不过要记得加上Exit,要不然下一行执行的时候会出错
return p.StandardOutput.ReadToEnd(); // 从输出流取得命令执行结果
}
五、取得屏幕拷贝 public Image GetScreen( )
{
//this.Hide();
IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
//创建显示器的DC
Graphics g1 = Graphics.FromHdc(dc1);
//由一个指定设备的句柄创建一个新的Graphics对象
Bitmap MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
//根据屏幕大小创建一个与之相同大小的Bitmap对象
Graphics g2 = Graphics.FromImage(MyImage);
//获得屏幕的句柄
IntPtr dc3 = g1.GetHdc();
//获得位图的句柄
IntPtr dc2 = g2.GetHdc();
//把当前屏幕捕获到位图对象中
BitBlt(dc2, , , Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc3, , , );
//把当前屏幕拷贝到位图中
g1.ReleaseHdc(dc3);
//释放屏幕句柄
g2.ReleaseHdc(dc2);
//释放位图句柄
return MyImage;
//this.Show();
}
取得屏幕拷贝的代码直接用了bitmap格式,性能不高,在实际使用中应该考虑进行压缩。
C# 实现远程控制软件的关键技术的更多相关文章
- 开源一款远程控制软件 —— pcshare
这里开放一款远程控制软件的源码--pcshare,该软件分为被控制端和控制端.部分界面如下: 控制端通过寄生在被控制端的后台程序来实现控制,可以对被控制台进行文件管理.屏幕监控.键盘监控.监控管理.查 ...
- 解读:20大5G关键技术
解读:20大5G关键技术 5G网络技术主要分为三类:核心网.回传和前传网络.无线接入网. 核心网 核心网关键技术主要包括:网络功能虚拟化(NFV).软件定义网络(SDN).网络切片和多接入边缘计算(M ...
- 小小知识点(二十七)20大5G关键技术
5G网络技术主要分为三类:核心网.回传和前传网络.无线接入网. 核心网 核心网关键技术主要包括:网络功能虚拟化(NFV).软件定义网络(SDN).网络切片和多接入边缘计算(MEC). 1 网络功能虚拟 ...
- 5G关键技术评述
业内重大事件: 张 平:无线通信领域专家,北京邮电大学教授,博士生导师,现任北京邮电大学无线新技术研究所(WTI)所长.泛网无线通信教育部重点实验室主任以及中德软件研究所副所长.张平教授是国家宽带无 ...
- 大型网站提速关键技术(页面静态化,memcached,MySql优化)(一)
一:关键技术介绍: 衡量是否为大型网站的要素: A:PV值(page views 页面浏览量) 访问量大: 带来的问题:1:流量大 -->解决方案:增加带宽,优化程序(视频和图片较浪费带宽,尽量 ...
- Java进阶(三)多线程开发关键技术
原创文章,同步发自作者个人博客,转载请务必以超链接形式在文章开头处注明出处http://www.jasongj.com/java/multi_thread/. sleep和wait到底什么区别 其实这 ...
- (1)RGB-D SLAM系列- 工具篇(硬件+关键技术)
/*************************************************************************************************** ...
- TeamViewer12.0.71503(远程控制软件)精简版单文件企业版介绍
TeamViewer 是一款能在任何防火墙和 NAT 代理的后台用于远程控制,桌面共享和文件传输的简单且快速的解决方案.为了连接到另一台计算机,只需要在两台计算机上同时运行 TeamViewer 即可 ...
- 操作PDF文件的关键技术点
一个PDF文档从大到小可以分成如下几个要素:文档.章节.小节.段落.表格.列表. com.lowagie.text.Document表示PDF文档.必须为它创建一个PDF写入器,即com.lowagi ...
随机推荐
- HDU 5266 pog loves szh III (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...
- UVaLive 7374 Racing Gems (DP,LIS)
题意:以辆赛车可以从x轴上任意点出发,他的水平速度允许他向每向上移动v个单位,就能向左或向右移动v/r个单位(也就是它的辐射范围是个等腰三角形) 现在赛车从x轴出发,问它在到达终点前能吃到的最多钻石. ...
- CodeForces 706B Interesting drink (二分查找)
题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个. 析:其实很简单么,先排序,然后十分查找,so easy. 代码如下: #pragma comment(lin ...
- hibernate[版本四]知识总结
1.hibernate是orm对象关系映射,是对jdbc的封装 2.hibernate版helloworld 2.1导入jar <dependencies> <dependency& ...
- 位运算&字节运算
- SQL存储过程调试
转自:http://www.cnblogs.com/xiangzhong/archive/2012/10/27/2742974.html 今天突然有同事问起,如何在sqlserver中调试存储过程(我 ...
- Debug with jdb
原文地址: http://www.javaworld.com/article/2077445/testing-debugging/debug-with-jdb.html Q: How do you u ...
- PostgreSQL的 initdb 源代码分析之九
继续:下面的是定义信号处理函数. /* * now we are starting to do real work, trap signals so we can clean up */ /* som ...
- android 手电筒的实现
android手机用闪光灯做成手电筒的应用非常多,可是有的不能用. 后来发现是除了把 camera device的 flashmode设置成torch外还要打开预览: 以下是代码: MainActiv ...
- xxx-servlet.xml vs applicationContext.xml
Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml def ...