首页  ·  知识 ·  编程语言
获取客户端桌面信息
网友  收集  .NET  编辑:德仔   图片来源:网络
客户端: dp-highlighter bar tools&

客户端:

C#代码 复制代码
  1. namespace NetClient   
  2. {   
  3.     public partial class Form1 : Form   
  4.     {   
  5.         public Form1()   
  6.         {   
  7.             InitializeComponent();   
  8.         }   
  9.   
  10.         IPAddress ip;   
  11.         int port;   
  12.         Thread th;   
  13.         Socket NetClient;   
  14.   
  15.         /// <summary>   
  16.         /// 连接   
  17.         /// </summary>   
  18.         /// <param name="sender"></param>   
  19.         /// <param name="e"></param>   
  20.         private void btnOpen_Click(object sender, EventArgs e)   
  21.         {   
  22.             try  
  23.             {   
  24.                 ip = IPAddress.Parse(txtAddress.Text);   
  25.                 port = Convert.ToInt32(numPort.Value);   
  26.                 btnOpen.Enabled = false;   
  27.                 th = new Thread(Connection);   
  28.                 th.Start();   
  29.             }   
  30.             catch (Exception ex)   
  31.             {   
  32.                 MessageBox.Show("IP地址格式输入不正确!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);   
  33.             }   
  34.         }   
  35.   
  36.         private void Connection()   
  37.         {   
  38.             NetClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);   
  39.             IPEndPoint ie = new IPEndPoint(ip, port);//服务器的IP和端口    
  40.             try  
  41.             {   
  42.                 //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。    
  43.                 NetClient.Connect(ie);   
  44.                 while (true)   
  45.                 {   
  46.                     byte[] dt = GetDesktop();   
  47.                     NetClient.Send(dt);   
  48.                     dt = null;   
  49.                     Thread.Sleep(500);   
  50.                 }   
  51.             }   
  52.             catch (SocketException e)   
  53.             {   
  54.                 MessageBox.Show("连接失败:" + e.Message);   
  55.             }   
  56.             if (NetClient.Connected)   
  57.             {   
  58.                 NetClient.Shutdown(SocketShutdown.Both);   
  59.                 NetClient.Close();   
  60.             }   
  61.         }   
  62.   
  63.         private byte[] GetDesktop()   
  64.         {   
  65.             Bitmap bmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);   
  66.             using (Graphics g = Graphics.FromImage(bmp))   
  67.             {   
  68.                 g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bmp.Size);   
  69.             }   
  70.             MemoryStream ms = new MemoryStream();   
  71.             bmp.Save(ms, ImageFormat.Jpeg);   
  72.             byte[] data = new byte[ms.Length];   
  73.             ms.Position = 0;   
  74.             ms.Read(data, 0, data.Length);   
  75.             ms.Close();   
  76.             return data;   
  77.         }   
  78.   
  79.         /// <summary>   
  80.         /// 断开   
  81.         /// </summary>   
  82.         /// <param name="sender"></param>   
  83.         /// <param name="e"></param>   
  84.         private void btnClose_Click(object sender, EventArgs e)   
  85.         {   
  86.             btnOpen.Enabled = true;   
  87.             if (NetClient != null)   
  88.                 NetClient.Close();   
  89.             if (th != null && th.IsAlive)   
  90.                 th.Abort();   
  91.         }   
  92.     }   
  93. }  

 

服务器端:

C#代码 复制代码
  1. namespace NETServer   
  2. {   
  3.     public delegate void ClientConnectionEventHandler(Socket socket, string ip, bool con);   
  4.     public delegate void DataReceiveEventHandler(byte[] data, string ip);   
  5.   
  6.     public partial class Form1 : Form   
  7.     {   
  8.         public Form1()   
  9.         {   
  10.             InitializeComponent();   
  11.             this.ClientConnection += new ClientConnectionEventHandler(Form1_ClientConnection);   
  12.             this.DataReceive += new DataReceiveEventHandler(Form1_DataReceive);   
  13.         }   
  14.   
  15.         void Form1_DataReceive(byte[] data, string ip)   
  16.         {   
  17.             if (tabControl1.InvokeRequired)   
  18.             {   
  19.                 tabControl1.Invoke(new DataReceiveEventHandler(Form1_DataReceive), data, ip);   
  20.             }   
  21.             else  
  22.             {   
  23.                 try  
  24.                 {   
  25.                     TabPage tb = FindTab(ip);   
  26.                     if (tb != null)   
  27.                     {   
  28.                         (tb.Controls[0] as PictureBox).Image = Image.FromStream(new MemoryStream(data));   
  29.                         data = null;   
  30.                     }   
  31.                 }   
  32.                 catch { }   
  33.             }   
  34.         }   
  35.   
  36.         void Form1_ClientConnection(Socket socket, string ip, bool con)   
  37.         {   
  38.             if (this.InvokeRequired)   
  39.             {   
  40.                 this.Invoke(new ClientConnectionEventHandler(Form1_ClientConnection), socket, ip, con);   
  41.             }   
  42.             else  
  43.             {   
  44.                 TabPage tp = FindTab(ip);   
  45.                 if (con)   
  46.                 {   
  47.                     Thread th = new Thread(ClientListen);   
  48.                     if (tp == null)   
  49.                         InitTab(ip, th);   
  50.                     th.Start(socket);   
  51.                 }   
  52.                 else  
  53.                 {   
  54.                     if (tp != null)   
  55.                     {   
  56.                         Thread t = tp.Tag as Thread;   
  57.                         if (t != null)   
  58.                             t.Abort();   
  59.                         tabControl1.TabPages.Remove(tp);   
  60.                     }   
  61.                 }   
  62.                 if (tabControl1.TabPages.Count > 0)   
  63.                     this.Text = string.Format("已连接,共有 {0} 个连接...", tabControl1.TabPages.Count);   
  64.                 else  
  65.                     this.Text = "当前没有客户端连接...";   
  66.             }   
  67.         }   
  68.   
  69.         private void InitTab(string ip, Thread th)   
  70.         {   
  71.             TabPage tab = new TabPage();   
  72.             PictureBox p = new PictureBox();   
  73.             p.Dock = DockStyle.Fill;   
  74.             p.SizeMode = PictureBoxSizeMode.StretchImage;   
  75.             tab.Controls.Add(p);   
  76.             p.Show();   
  77.             tab.Tag = th;   
  78.             tab.Text = ip;   
  79.             this.tabControl1.TabPages.Add(tab);   
  80.         }   
  81.   
  82.         public event ClientConnectionEventHandler ClientConnection;   
  83.         private void ConnectionProxy(Socket socket, string ip, bool con)   
  84.         {   
  85.             if (ClientConnection != null)   
  86.                 ClientConnection(socket, ip, con);   
  87.         }   
  88.   
  89.         public event DataReceiveEventHandler DataReceive;   
  90.         private void ReceiveProxy(byte[] data, string ip)   
  91.         {   
  92.             if (DataReceive != null)   
  93.                 DataReceive(data, ip);   
  94.         }   
  95.   
  96.   
  97.         Thread th;   
  98.         Socket NetSocket;   
  99.         private void Form1_Load(object sender, EventArgs e)   
  100.         {   
  101.             this.Text = "正在监听,等待客户端连接...";   
  102.             th = new Thread(InitServer);   
  103.             th.Start();   
  104.         }   
  105.   
  106.         private TabPage FindTab(string ip)   
  107.         {   
  108.             foreach (TabPage tp in tabControl1.TabPages)   
  109.             {   
  110.                 if (tp.Text == ip)   
  111.                     return tp;   
  112.             }   
  113.             return null;   
  114.         }   
  115.   
  116.         private void InitServer()   
  117.         {   
  118.             int port = 2000;   
  119.             IPAddress ip = null;   
  120.             IPAddress[] id = Dns.GetHostAddresses("");   
  121.             foreach (IPAddress i in id)   
  122.             {   
  123.                 Regex reg = new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");   
  124.                 if (!i.IsIPv6LinkLocal && reg.IsMatch(i.ToString()))   
  125.                 {   
  126.                     ip = i;   
  127.                     break;   
  128.                 }   
  129.             }   
  130.             if (ip == null)   
  131.                 ip = IPAddress.Parse("127.0.0.1");   
  132.             IPEndPoint ipep = new IPEndPoint(ip, port);//用指定的端口和ip初始化IPEndPoint类的新实例    
  133.             NetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);   
  134.             NetSocket.Bind(ipep);   
  135.             while (true)   
  136.             {   
  137.                 NetSocket.Listen(10);   
  138.                 Socket client = NetSocket.Accept();   
  139.                 IPEndPoint clientip = (IPEndPoint)client.RemoteEndPoint;   
  140.                 ConnectionProxy(client, clientip.Address.ToString(), true);   
  141.             }   
  142.         }   
  143.   
  144.         private void ClientListen(object obj)   
  145.         {   
  146.             Socket client = obj as Socket;   
  147.             if (client == null)   
  148.                 return;   
  149.             while (true)   
  150.             {//循环来不断的从客户端获取信息   
  151.                 byte[] data = new byte[200000];   
  152.                 int bf = client.Receive(data);   
  153.                 if (bf == 0)   
  154.                 {   
  155.                     ConnectionProxy(client, ((IPEndPoint)client.RemoteEndPoint).Address.ToString(), false);   
  156.                     break;   
  157.                 }   
  158.                 ReceiveProxy(data, ((IPEndPoint)client.RemoteEndPoint).Address.ToString());   
  159.             }   
  160.         }   
  161.   
  162.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)   
  163.         {    
  164.             if (NetSocket != null)   
  165.                 NetSocket.Close();   
  166.             if (th != null && th.IsAlive)   
  167.                 th.Abort();   
  168.             if (MessageBox.Show("确定要断开客户端的连接吗?""提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)   
  169.                 e.Cancel = true;   
  170.         }   
  171.   
  172.     }   
  173. }  
  174.  
本文作者:网友 来源:网络收集
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的