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 ...
随机推荐
- iomanip.h
http://baike.baidu.com/link?url=zuNLgcUVylhUYYefyV13F38NChIMx8nnCEWV5zkkTQMrrSdKPxUERZuydSHtp6sXukWv ...
- linux sar 命令详解
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...
- Quality Center 使用IE8异常浏览器打开
需要装2个软件 1. Microsoft Visual C++ 2005 Redistributable Package (x64) 2. dotnetfx35.exe 配置IE的选项 使用兼容模 ...
- 亲和串(HDU2203)
http://acm.hdu.edu.cn/showproblem.php?pid=2203 题目意思很简单,求s1串所构成的环中是否有s2这个串 用CMP参考http://s.acmore.net/ ...
- F - Coins
F - Coins Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit St ...
- 在线教育服务:http://www.ablesky.com/
在线教育服务:http://www.ablesky.com/
- 允许ubuntu下mysql远程连接
第一步: gedit /etc/mysql/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0.0.1 或者改为: bind- ...
- 安卓任意两个或多个Fragment之间的交互与刷新界面
平时项目中遇到一个问题:在子fragment中刷新父fragment的界面,通俗的说也就是在任何一个fragment中来刷新另一个fragment.大家都知道activity和fragment之间的交 ...
- SSH登录失败:Host key verification failed
转载自:https://help.aliyun.com/knowledge_detail/41471.html 注意:本文相关 Linux 配置及说明已在 CentOS 6.5 64 位操作系统中进行 ...
- idea 搭建java项目
IntelliJ IDEA 12.0搭建Maven Web SSH2架构项目示例 以IDEA为环境,搭建SSH架构示例程序,用Maven管理依赖.这篇文章是一个示例,你需要首先搭建好M ...