news 2026/4/4 3:08:07

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/2 8:46:05

EmotiVoice能否用于远程医疗语音通知系统?HIPAA合规考量

EmotiVoice在远程医疗语音通知系统中的应用与HIPAA合规路径 在慢性病管理日益依赖数字化工具的今天&#xff0c;一个高血压患者清晨醒来&#xff0c;手机自动播放一条语音&#xff1a;“李老师&#xff0c;昨晚您的血压监测显示有两次超过150/95 mmHg&#xff0c;建议您今天上…

作者头像 李华
网站建设 2026/4/1 2:42:19

单词倒排 和 字符串P型编码

单词倒排这道题只需要用getline获取输入然后字符串遍历存入一个临时字符串&#xff0c;每当遇到空格便将字符串添加到out字符串前面即可。#include<bits/stdc.h> using namespace std; int main(){string in,out;string now "";getline(cin,in);for(char &…

作者头像 李华
网站建设 2026/3/31 3:29:38

9个AI论文工具,MBA学生高效写作必备!

9个AI论文工具&#xff0c;MBA学生高效写作必备&#xff01; AI 工具如何重塑论文写作的效率与质量 在当今快节奏的学术环境中&#xff0c;MBA 学生面临着日益繁重的论文写作任务。无论是案例分析、商业计划书&#xff0c;还是研究报告&#xff0c;都需要高质量的内容输出和严谨…

作者头像 李华
网站建设 2026/3/4 22:28:52

从0到1亚马逊、temu、速卖通卖家如何实现自养号测评采购自由?

在当今竞争激烈的跨境电商领域&#xff0c;搭建亚马逊、速卖通、temu等平台的测评系统&#xff0c;已成为众多商家提升店铺竞争力的关键手段。然而&#xff0c;这一过程极为复杂精细&#xff0c;涵盖众多环节与技术要点。以下是一份源自权威渠道的亚马逊测评系统搭建秘籍&#…

作者头像 李华
网站建设 2026/3/13 8:19:13

EmotiVoice语音合成引擎:打造富有情感的TTS体验

EmotiVoice语音合成引擎&#xff1a;打造富有情感的TTS体验 在虚拟助手越来越“能说会道”的今天&#xff0c;用户早已不满足于那种机械朗读式的语音输出。我们期待的是一个能“共情”的声音——当你疲惫时它语气温柔&#xff0c;当剧情紧张时它语气急促&#xff0c;甚至能在一…

作者头像 李华
网站建设 2026/3/24 1:32:45

Windows程序资源编辑神器rcedit:告别繁琐的图形界面操作

Windows程序资源编辑神器rcedit&#xff1a;告别繁琐的图形界面操作 【免费下载链接】rcedit Command line tool to edit resources of exe 项目地址: https://gitcode.com/gh_mirrors/rc/rcedit 在Windows开发的世界里&#xff0c;你是否曾经为修改一个简单的程序图标而…

作者头像 李华