您的位置:首页 > 编程语言 > C#

C#实现远程控制及桌面监控功能(服务端)

2013-06-27 10:34 701 查看
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Threading;

using System.IO;

using System.Drawing.Imaging;

using System.Net;

using System.Net.Sockets;

namespace TestOnlineTeather

{

public partial class Form1 : Form

{

string pro = "{0}&{1}&{2}&{3}&{4}";//我的软件的协传输议

string userName = Dns.GetHostName();// 获取我的主机名

IPAddress ip;//我的ip

int port = 9305;//我的端口号

IPAddress stuIp; //学生的广播ip

public Form1()

{

InitializeComponent();

}

private void button2_Click(object sender, EventArgs e)

{

if (MessageBox.Show("结束学生考试?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)

{

//结束

UdpSendMsg(stuIp, "2", "结束");

}

}

public void UdpSendMsg(IPAddress sendIp, string cmdNo, string msg)

{

Socket client = new Socket(AddressFamily.InterNetwork,

SocketType.Dgram, ProtocolType.Udp);

//发送的内容

string context = string.Format(pro, userName,

ip.ToString(), DateTime.Now.Ticks, cmdNo, msg);

byte[] buffer = Encoding.Default.GetBytes(context);

//发给谁

client.Connect(sendIp, port);

//发送

client.Send(buffer);

client.Close();

}

private void button1_Click(object sender, EventArgs e)

{

//考试

UdpSendMsg(stuIp, "1", "考试");

Thread.Sleep(500);

//接收广播*************************

Thread th = new Thread(new ThreadStart(ReciveData));

th.IsBackground = true;

th.Start();

}

public void ReciveData()

{

IPAddress ip = IPAddress.Any;

Socket server = new Socket(AddressFamily.InterNetwork,

SocketType.Dgram, ProtocolType.Udp);

EndPoint ipe = new IPEndPoint(ip, port);

server.Bind(ipe);

byte[] buffer = new byte[60000];

//int width=Screen.PrimaryScreen .Bounds .Width;

//int height=Screen.PrimaryScreen.Bounds.Height;

while (true)

{

// Bitmap bmp = new Bitmap(width, height);

MemoryStream ms = new MemoryStream();

int len = server.Receive(buffer);

ms.Write(buffer, 0, len);

while (true)

{

if (len == 1 && buffer[0] == 100)

{

break;

}

len = server.Receive(buffer);

ms.Write(buffer, 0, len);

}

//流里面存放了一个图片的所有字节

Bitmap bmp = new Bitmap(ms);

pictureBox1.Image = bmp;

}

}

private void Form1_Load(object sender, EventArgs e)

{

#region //****************初始化用户信息

string hostName = Dns.GetHostName();

IPAddress[] ips = Dns.GetHostAddresses(hostName);

foreach (IPAddress item in ips)

{

if (!item.IsIPv6LinkLocal)

{

ip = item;

}

}

string t=textBox1.Text ;

stuIp = IPAddress.Parse("192.168." + t + ".255");

#endregion //*********************************

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐