news 2026/4/23 9:51:28

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/23 11:30:16

Contest1110 - 河南工大2025新生周赛(8)-赛后总结

Contest1110 - 河南工大2025新生周赛&#xff08;8&#xff09;——命题人&#xff1a;庞贺航、高旭 - HAUTOJ A 签到题&#xff1f; 读题目&#xff0c;注意到α和β的值都为一&#xff0c;现要求二者相加的值&#xff08;经典线性求和&#xff09;&#xff0c;输出2即可 1…

作者头像 李华
网站建设 2026/4/23 12:52:20

EmotiVoice商业应用场景全景图:覆盖10大行业解决方案

EmotiVoice商业应用场景全景图&#xff1a;覆盖10大行业解决方案 在智能语音技术不断渗透日常生活的今天&#xff0c;用户早已不再满足于“能说话”的机器。当客服用毫无起伏的语调回应投诉、当有声书朗读像电子闹钟报时、当虚拟偶像直播依赖真人配音轮班上阵——这些场景背后&…

作者头像 李华
网站建设 2026/4/23 11:34:26

AI赋能智慧客服与人工客服融合系统企业级方案

文章目录 1. 项目概述与架构设计1.1 系统核心设计理念1.2 整体架构图1.3 技术栈选择 2. 环境搭建与项目初始化2.1 开发环境配置2.2 配置文件设计 3. 核心数据模型设计3.1 数据库模型定义3.2 数据库初始化脚本 4. AI核心组件实现4.1 NLP处理器&#xff08;意图识别与情感分析&am…

作者头像 李华
网站建设 2026/4/23 14:46:58

EmotiVoice语音呼吸感模拟技术增加真实度

EmotiVoice语音呼吸感模拟技术增加真实度 在虚拟主播流畅播报新闻、AI助手温柔提醒日程的今天&#xff0c;我们或许已经习惯了这些“非人类”的声音。但有没有一瞬间&#xff0c;你觉得它们说得太完美了&#xff1f;完美到不像真人——从不喘气、没有停顿、情绪永远平稳。这种“…

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

EmotiVoice技术解析:多情感语音合成背后的秘密

EmotiVoice技术解析&#xff1a;多情感语音合成背后的秘密 在虚拟助手开始对你“冷笑”、游戏角色因剧情转折而声音颤抖的今天&#xff0c;我们早已不再满足于那种机械朗读式的AI语音。用户想要的是有温度的声音——能愤怒、会撒娇、甚至带着一丝疲惫的叹息。正是在这种需求驱动…

作者头像 李华