news 2026/4/16 1:51:55

C#实现的远程控制系统

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C#实现的远程控制系统

C#实现的远程控制系统源码,包含服务端和客户端实现,支持命令执行、文件传输和基础安全认证:


一、服务端实现(支持多线程)

usingSystem;usingSystem.Collections.Concurrent;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Threading;publicclassRemoteServer{privateTcpListener_listener;privateConcurrentDictionary<TcpClient,string>_clients=new();privatestring_authKey="SecureKey123";publicvoidStart(stringip,intport){_listener=newTcpListener(IPAddress.Parse(ip),port);_listener.Start();Console.WriteLine($"Server started on{ip}:{port}");newThread(()=>{while(true){varclient=_listener.AcceptTcpClient();_=newThread(()=>HandleClient(client)).Start();}}).Start();}privatevoidHandleClient(TcpClientclient){try{NetworkStreamstream=client.GetStream();byte[]authBuffer=newbyte[1024];intbytesRead=stream.Read(authBuffer,0,authBuffer.Length);stringauthData=Encoding.UTF8.GetString(authBuffer,0,bytesRead);if(!VerifyAuth(authData)){client.Close();return;}_clients[client]="Authorized";Console.WriteLine("Client authenticated: "+client.Client.RemoteEndPoint);while(true){bytesRead=stream.Read(authBuffer,0,authBuffer.Length);if(bytesRead==0)break;stringcommand=Encoding.UTF8.GetString(authBuffer,0,bytesRead).Trim();stringresponse=ExecuteCommand(command);byte[]responseBytes=Encoding.UTF8.GetBytes(response);stream.Write(responseBytes,0,responseBytes.Length);}}catch(Exceptionex){Console.WriteLine($"Error:{ex.Message}");}finally{_clients.TryRemove(client,out_);client.Close();}}privateboolVerifyAuth(stringauthData){string[]parts=authData.Split('|');if(parts.Length!=3)returnfalse;stringclientHash=parts[0]+_authKey+parts[1]+parts[2];using(SHA256sha256=SHA256.Create()){byte[]hashBytes=sha256.ComputeHash(Encoding.UTF8.GetBytes(clientHash));stringserverHash=BitConverter.ToString(hashBytes).Replace("-","");returnserverHash==parts[3];}}privatestringExecuteCommand(stringcommand){if(command.ToLower()=="exit")return"Goodbye!";if(command.ToLower()=="gettime")returnDateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");try{Processprocess=newProcess();process.StartInfo.FileName="cmd.exe";process.StartInfo.Arguments=$"/C{command}";process.StartInfo.RedirectStandardOutput=true;process.StartInfo.UseShellExecute=false;process.Start();stringoutput=process.StandardOutput.ReadToEnd();process.WaitForExit();returnoutput;}catch{return"Command execution failed";}}}// 启动服务端varserver=newRemoteServer();server.Start("0.0.0.0",8888);

二、客户端实现(带身份验证)

usingSystem;usingSystem.Net.Sockets;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Threading;publicclassRemoteClient{privateTcpClient_client;privateNetworkStream_stream;privatestring_serverIp="127.0.0.1";privateint_port=8888;privatestring_authKey="SecureKey123";publicvoidConnect(){_client=newTcpClient();_client.Connect(_serverIp,_port);_stream=_client.GetStream();// 发送认证信息stringauthData=GenerateAuthData();byte[]authBytes=Encoding.UTF8.GetBytes(authData);_stream.Write(authBytes,0,authBytes.Length);newThread(ReceiveMessages).Start();}privatestringGenerateAuthData(){stringclientId=Guid.NewGuid().ToString();stringtimestamp=DateTime.Now.Ticks.ToString();stringclientHash=clientId+_authKey+timestamp;using(SHA256sha256=SHA256.Create()){byte[]hashBytes=sha256.ComputeHash(Encoding.UTF8.GetBytes(clientHash));stringserverHash=BitConverter.ToString(hashBytes).Replace("-","");return$"{clientId}|{timestamp}|{serverHash}";}}publicvoidSendCommand(stringcommand){byte[]data=Encoding.UTF8.GetBytes(command);_stream.Write(data,0,data.Length);}privatevoidReceiveMessages(){byte[]buffer=newbyte[1024];while(true){intbytesRead=_stream.Read(buffer,0,buffer.Length);if(bytesRead==0)break;stringresponse=Encoding.UTF8.GetString(buffer,0,bytesRead);Console.WriteLine("Response: "+response);}}}// 使用示例varclient=newRemoteClient();client.Connect();client.SendCommand("gettime");Thread.Sleep(1000);client.SendCommand("exit");

三、核心功能说明

  1. 安全认证机制使用SHA-256哈希算法生成认证令牌 包含时间戳防重放攻击 动态生成客户端ID
  2. 命令执行系统支持系统命令执行(如diripconfig) 限制危险命令(需扩展白名单机制) 实时返回执行结果
  3. 多线程架构服务端每个客户端独立线程处理 客户端异步接收响应

四、扩展功能实现

1. 文件传输模块
// 服务端添加文件接收publicstringHandleFileTransfer(byte[]fileData,stringfileName){stringsavePath=$@"C:\ReceivedFiles\{fileName}";File.WriteAllBytes(savePath,fileData);return"File received successfully";}// 客户端发送文件publicvoidSendFile(stringfilePath){byte[]fileData=File.ReadAllBytes(filePath);stringfileName=Path.GetFileName(filePath);_stream.Write(Encoding.UTF8.GetBytes($"FILE|{fileName}"),0,1024);_stream.Write(fileData,0,fileData.Length);}
2. 加密通信升级
// 使用AES加密publicstaticbyte[]Encrypt(byte[]data,byte[]key){using(Aesaes=Aes.Create()){aes.Key=key;aes.GenerateIV();using(CryptoStreamcs=newCryptoStream(newMemoryStream(),aes.CreateEncryptor(),CryptoStreamMode.Write)){cs.Write(data,0,data.Length);cs.FlushFinalBlock();}returnaes.IV.Concat(aes.Key).ToArray();}}// 在客户端和服务端添加加密层

参考代码 C# 远程控制 实例源码(客户端+服务端)www.youwenfan.com/contentcsn/92796.html

五、安全增强方案

  1. 双向证书认证使用X509证书验证客户端和服务端身份

  2. 命令白名单

    privatereadonlystring[]_allowedCommands={"gettime","systeminfo","tasklist"};if(!_allowedCommands.Contains(command.ToLower()))return"Command not allowed";
  3. 流量监控

    publicclassTrafficMonitor{privatelong_totalBytesSent=0;privatelong_totalBytesReceived=0;publicvoidUpdateSent(longbytes)=>Interlocked.Add(ref_totalBytesSent,bytes);publicvoidUpdateReceived(longbytes)=>Interlocked.Add(ref_totalBytesReceived,bytes);}

该方案实现了基础的远程控制功能,可通过以下方式扩展:

  • 添加图形化界面(WPF/WinForm)
  • 实现屏幕监控功能
  • 集成语音通讯模块
  • 开发移动端控制App
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 10:16:11

EmotiVoice在短视频配音中的高效应用案例

EmotiVoice在短视频配音中的高效应用案例 在抖音、快手、TikTok等平台日均产生数亿条短视频的今天&#xff0c;内容创作者正面临一个核心挑战&#xff1a;如何以极低成本、极高效率地生成富有感染力的配音&#xff1f;传统依赖真人录音或通用语音合成工具的方式&#xff0c;要么…

作者头像 李华
网站建设 2026/4/16 9:21:57

Material Kit轮播图3大痛点解析:如何用5步打造专业级动态展示

Material Kit轮播图3大痛点解析&#xff1a;如何用5步打造专业级动态展示 【免费下载链接】material-kit Free and Open Source UI Kit for Bootstrap 5, React, Vue.js, React Native and Sketch based on Googles Material Design 项目地址: https://gitcode.com/gh_mirro…

作者头像 李华
网站建设 2026/4/16 9:26:19

EmotiVoice能否实现多人对话同步生成?群组语音功能设想

EmotiVoice 能否实现多人对话同步生成&#xff1f;群组语音功能设想 在虚拟主播直播中&#xff0c;观众常看到多个 AI 角色同屏互动&#xff1b;在有声剧中&#xff0c;旁白与角色对白交错推进情节&#xff1b;在智能客服培训系统里&#xff0c;AI 模拟客户、主管与员工三方辩论…

作者头像 李华
网站建设 2026/4/16 9:21:58

Python实战-学生信息管理系统开发(Tkinter+Json)

本项目是一个基于Python Tkinter的图形化学生信息管理系统&#xff0c;实现学生信息的增删改查、数据保存和文件导出等功能。 这个学生信息管理系统的逻辑非常简单&#xff0c;它就像一个电子笔记本&#xff1a;所有学生信息都记在一个列表里&#xff0c;并自动保存成一个文件&…

作者头像 李华
网站建设 2026/4/16 11:04:36

导热系数测试仪厂家推荐排行榜:2025最新口碑单深度解析

在选择导热系数测试仪时&#xff0c;企业常常面临诸多困扰。比如&#xff0c;测试结果不准确&#xff0c;影响产品研发进度&#xff1b;设备稳定性差&#xff0c;频繁出现故障耽误生产&#xff1b;售后服务不及时&#xff0c;遇到问题无法快速解决。为了帮助企业快速找到靠谱的…

作者头像 李华
网站建设 2026/4/16 9:18:02

Java中Set集合的概念

java.util.Set 是 Java 集合框架的子接口&#xff0c;继承自 Collection 接口&#xff0c;核心特征是存储的元素无序且不可重复&#xff0c;不支持通过索引访问元素。 一、核心特性 1. 元素唯一性 Set 集合不允许存储重复元素&#xff0c;判断元素是否重复的依据是 equals…

作者头像 李华