实例介绍
【实例简介】
【实例截图】
【核心代码】
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; namespace UDPMessager { public partial class Form1 : Form { UDPSocket _udpSocket = null; //数据接收 UDPSender _udpSender = null; //数据发送 MyDataAnalyse _myDataAnalyse = null; //数据分析 MyDataDeal _myDataDeal = null; //数据处理 public Form1() { InitializeComponent(); Init(); } private void Init() { //使各环节关联起来 _udpSocket = new UDPSocket(); _udpSender = new UDPSender(_udpSocket); _myDataAnalyse = new MyDataAnalyse(_udpSocket); _myDataDeal = new MyDataDeal(_myDataAnalyse); _myDataDeal.Login = new LoginEventHandler(_myDataDeal_Login); _myDataDeal.LoginBack = new LoginBackEventHandler(_myDataDeal_LoginBack); _myDataDeal.Logout = new LogoutEventHandler(_myDataDeal_Logout); _myDataDeal.TxtMessageBack = new TxtMessageBackEventHandler(_myDataDeal_TxtMessageBack); _myDataDeal.TxtMessageReceived = new TxtMessageReceivedEventHandler(_myDataDeal_TxtMessageReceived); _myDataDeal.AskSendFile = new AskSendFileEventHandler(_myDataDeal_AskSendFile); Application.ThreadExit = new EventHandler(Application_ThreadExit); } void Application_ThreadExit(object sender, EventArgs e) { _udpSender.SendString(Msg.ZMsg5, "", "255.255.255.255", _udpSocket.Port); //退出 } private void Form1_Load(object sender, EventArgs e) { _myDataDeal.Start(); //开启数据处理环节 _myDataAnalyse.Start(); //开启数据分析环节 if (!_udpSocket.Start()) { MessageBox.Show("端口绑定失败!"); Environment.Exit(0); }//开启数据接收环节 listView1.Items.Clear(); //发送登录信息 _udpSender.SendString(Msg.ZMsg1, Helper.GetUserName() "^" Helper.GetComputerName() "^" Helper.GetGroupName(), "255.255.255.255", _udpSocket.Port); //广播 } #region udp event handlers /// <summary> /// 接收文本信件 /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> /// <param name="txtMsg"></param> void _myDataDeal_TxtMessageReceived(string remoteIP, int remotePort, string txtMsg) { this.BeginInvoke((Action)delegate() { foreach (ListViewItem item in listView1.Items) { if (item.SubItems[3].Text == remoteIP) { frmMsg frmm = new frmMsg(remoteIP, remotePort, txtMsg, item.SubItems[1].Text, item.SubItems[2].Text, _udpSender); frmm.Reply = new ReplyEventHandler(frmm_Reply); frmm.Show(); } } }); } /// <summary> /// 信件反馈 /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> /// <param name="txtMsgBack"></param> void _myDataDeal_TxtMessageBack(string remoteIP, int remotePort, string txtMsgBack) { this.BeginInvoke((Action)delegate() { foreach (ListViewItem item in listView1.Items) { if (item.SubItems[3].Text == remoteIP) { MessageBox.Show(item.SubItems[1].Text "(" item.SubItems[2].Text ") 打开信封\n信件内容:\n" txtMsgBack); break; } } }); } /// <summary> /// 退出 /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> /// <param name="remoteName"></param> void _myDataDeal_Logout(string remoteIP, int remotePort, string remoteName) { string[] infos = remoteName.Split('^'); //if (infos.Length == 3) //{ this.BeginInvoke((Action)delegate() //这个可以确定不是在UI线程上执行 因此需要投递到UI线程上 { foreach (ListViewItem i in listView1.Items) { if (i.SubItems[3].Text.ToString() == remoteIP) { listView1.Items.Remove(i); label1.Text = listView1.Items.Count.ToString() "人在线"; break; } } }); //} } /// <summary> /// 登录反馈 /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> /// <param name="remoteName"></param> void _myDataDeal_LoginBack(string remoteIP, int remotePort, string remoteName) { string[] infos = remoteName.Split('^'); if (infos.Length == 3) { this.BeginInvoke((Action)delegate() //这个可以确定不是在UI线程上执行 因此需要投递到UI线程上 { bool flag = false; foreach (ListViewItem i in listView1.Items) { if (i.SubItems[3].Text.ToString() == remoteIP) { flag = true; break; } } if (!flag) { ListViewItem item = new ListViewItem(); item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[0]));//登录名 item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[1]));//主机名 item.SubItems.Add(new ListViewItem.ListViewSubItem(item, remoteIP)); //远程IP item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[2])); //组名 listView1.Items.Add(item); label1.Text = listView1.Items.Count.ToString() "人在线"; } }); } } /// <summary> /// 登录 /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> /// <param name="remoteName"></param> void _myDataDeal_Login(string remoteIP, int remotePort, string remoteName) { string[] infos = remoteName.Split('^'); if (infos.Length == 3) { this.BeginInvoke((Action)delegate() //这个可以确定不是在UI线程上执行 因此需要投递到UI线程上 { bool flag = false; foreach (ListViewItem i in listView1.Items) { if (i.SubItems[3].Text.ToString() == remoteIP) { flag = true; break; } } if (!flag) { ListViewItem item = new ListViewItem(); item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[0]));//登录名 item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[1]));//主机名 item.SubItems.Add(new ListViewItem.ListViewSubItem(item, remoteIP)); //远程IP item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[2])); //组名 listView1.Items.Add(item); label1.Text = listView1.Items.Count.ToString() "人在线"; } }); _udpSender.SendString(Msg.ZMsg2, Helper.GetUserName() "^" Helper.GetComputerName() "^" Helper.GetGroupName(), remoteIP, remotePort); //登录反馈 } } /// <summary> /// 请求发送文件 /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> /// <param name="filename"></param> /// <param name="length"></param> /// <param name="uid"></param> void _myDataDeal_AskSendFile(string remoteIP, int remotePort, string filename, long length, int uid) { this.BeginInvoke((Action)delegate() { foreach(ListViewItem item in listView1.Items) { if(item.SubItems[3].Text == remoteIP) { string username = item.SubItems[1].Text; string machinename = item.SubItems[2].Text; frmReceiveFile frmr = new frmReceiveFile(uid, length, remoteIP, _udpSocket.Port, username, machinename, filename, _udpSender, _myDataDeal); frmr.Show(); break; } } }); } #endregion /// <summary> /// 刷新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { listView1.Items.Clear(); //发送登录信息 _udpSender.SendString(Msg.ZMsg1, Helper.GetUserName() "^" Helper.GetComputerName() "^" Helper.GetGroupName(), "255.255.255.255", _udpSocket.Port); //广播 } /// <summary> /// 关于 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { MessageBox.Show("UDPMessager通信demo,详细参见 http://www.cnblogs.com/xiaozhi_5638/"); } /// <summary> /// 发送消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { if (textBox1.Text != "") { string pass = ""; if (checkBox2.Checked) { using (frmLock frml = new frmLock()) { if (frml.ShowDialog() == DialogResult.OK) { pass = frml.Pass; } } } string msg = pass "^" textBox1.Text; //为了方便简单相加 实际中不应该如此编程 string ip = ""; if (checkBox1.Checked) { ip = "255.255.255.255"; } else { ip = listView1.SelectedItems[0].SubItems[3].Text; } _udpSender.SendString(Msg.ZMsg3, msg, ip, _udpSocket.Port); //发送文本消息 checkBox1.Checked = false; checkBox2.Checked = false; textBox1.Text = ""; } } } /// <summary> /// 点击回复 /// </summary> /// <param name="oldmsg"></param> /// <param name="remoteIP"></param> void frmm_Reply(string oldmsg, string remoteIP) { foreach (ListViewItem item in listView1.Items) { if (item.SubItems[3].Text == remoteIP) { item.Selected = true; break; } } textBox1.Text = oldmsg; //引用原文 this.textBox1.Select(textBox1.Text.Length, 0); this.textBox1.Select(); } private void checkBox2_CheckedChanged(object sender, EventArgs e) { } /// <summary> /// 发送文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Multiselect = false; ofd.CheckFileExists = true; if (ofd.ShowDialog() == DialogResult.OK) { string remoteip = listView1.SelectedItems[0].SubItems[3].Text; string username = listView1.SelectedItems[0].SubItems[1].Text; string machinename = listView1.SelectedItems[0].SubItems[2].Text; frmSendFile frms = new frmSendFile(remoteip, _udpSocket.Port, username, machinename, ofd.FileName, _udpSender, _myDataDeal); frms.Show(); } } } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论