您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 仿飞秋,局域网聊天工具
  所属分类: 网络基础
  开发工具:
  文件大小: 80kb
  下载次数: 0
  上传时间: 2013-01-03
  提 供 者: tebie*****
 详细说明: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int port = 49151; private UdpClient udpclient; delegate void AppendString(sendData sendDatas);//添加用户列表委托 AppendString appendString; delegate void AppendStrings(string text);//添加广播消息委托 AppendStrings appendStrings; delegate void AppendStringGB(sendData text);//添加私人消息委托 AppendStringGB appendStringGB; public sendData sendDatas = new sendData(); IPHostEntry myentry; IPAddress myIp; Thread MyRecData; private void Form1_Load(object sender, EventArgs e) { udpclient = new UdpClient(port); myentry = Dns.GetHostEntry(Dns.GetHostName()); IPselect(); sendDatas.fromIP = myIp; sendDatas.toIP = IPAddress.Parse("255.255.255.255"); sendDatas.content = ""; appendString = new AppendString(AppString); appendStrings = new AppendStrings(AppStrings); appendStringGB = new AppendStringGB(AppStringGB); MyRecData = new Thread(new ThreadStart(RecData)); MyRecData.IsBackground = true; MyRecData.Start(); States(); } /// /// 广播本机IP /// public void States() { IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port); byte[] sen = Class2.senDataToByte(sendDatas); udpclient.Send(sen, sen.Length, iep); } /// /// 获取本机IP /// public void IPselect()//查找本机第一个IP地址 { string strHostName; strHostName = Dns.GetHostName(); Console.WriteLine("本机名:{0}", strHostName); IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); IPAddress[] ipAdd = ipEntry.AddressList; foreach (IPAddress ip in ipAdd) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { myIp = ip; return; } } } /// /// 私聊消息写入消息框 /// /// private void AppString(sendData sendDatas) { int i = forms(sendDatas.fromIP); if (arryFrom[i].Forms.InvokeRequired == true) { arryFrom[i].Forms.Invoke(appendString, sendDatas); } else { arryFrom[i].Forms.textBox1.AppendText("信息来自"+sendDatas.fromIP+":\r\n"+sendDatas.content + "\r\n"); } } /// /// 广播消息写入 /// /// private void AppStringGB(sendData text) { if (textBox1.InvokeRequired == true) { this.Invoke(appendStringGB, text); } else { textBox1.AppendText("消息来自"+text.fromIP+":\r\n"+text.content+"\r\n"); } } /// /// 判断接收的私聊消息是否有窗体,有就反回是哪个窗体。没有就创建一个窗体反悔集合最后一个索引 /// /// /// public int forms(IPAddress sens) { int f = 0; foreach (ArraysForm af in arryFrom) { if(sens.Equals(af.ToIp)) { return f; } f++; } MethodInvoker mi = new MethodInvoker(this.PerChat);//使用MethodInvoker委托跨线程访问 this.BeginInvoke(mi); Thread.Sleep(100); return arryFrom.Count-1; } /// /// 添加发言者的IP到用户列表有的将不添加 /// /// private void AppStrings(string text) { foreach(string str in listBox1.Items) { if(str.Equals(text)) { return; } } if (listBox1.InvokeRequired == true)//使用委托跨线程访问 { this.Invoke(appendStrings, text); } else { listBox1.Items.Add(text); States(); } } sendData sendsa; /// /// 接收广播消息 /// private void RecData() { IPEndPoint remote = null; while (true) { try { byte[] bytes = udpclient.Receive(ref remote); sendsa = Class2.byteTosendData(bytes); AppStrings(sendsa.fromIP.ToString()); string str = sendsa.content; string form = sendsa.fromIP.ToString(); if (str != ""&&sendsa;.toIP.Equals(IPAddress.Parse("255.255.255.255"))) { AppStringGB(sendsa); } if(sendsa.toIP.Equals(myIp)) { AppString(sendsa); } } catch(Exception e) { //MessageBox.Show(e.Message); break; } } } private void button1_Click(object sender, EventArgs e) { Sees(); } /// /// 广播消息 /// public void Sees() { UdpClient myUdpClient = new UdpClient(); try { sendData sens = new sendData(); IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port); sens.fromIP = myIp; sens.toIP = IPAddress.Parse("255.255.255.255"); sens.content = textBox2.Text; byte[] bytes = Class2.senDataToByte(sens); myUdpClient.Send(bytes, bytes.Length, iep); textBox2.Clear(); myUdpClient.Close(); } catch { MessageBox.Show("发送失败!"); } finally { myUdpClient.Close(); } } /// /// 关闭窗口 /// /// /// private void Form1_FormClosing(object sender, FormClosingEventArgs e) { udpclient.Close(); } /// /// 双击用户可以私聊 /// /// /// private void listBox1_DoubleClick(object sender, EventArgs e) { sendsa.fromIP = IPAddress.Parse(listBox1.Text); PerChat(); } /// /// 右键点击聊天可以私聊 /// /// /// private void 发送消息ToolStripMenuItem_Click(object sender, EventArgs e) { sendsa.fromIP = IPAddress.Parse(listBox1.Text); PerChat(); } public static List arryFrom = new List();// 装私聊窗口的集合 PersonalChat[] perChat=new PersonalChat[100];//私聊窗口数组 ArraysForm aForm;//私聊窗体类,包含聊天对象IP和窗体 int index = 0; /// /// 新建私聊窗口窗口, /// public void PerChat() { sendData preChatsenData = new sendData(); preChatsenData.fromIP = myIp; preChatsenData.toIP = sendsa.fromIP; preChatsenData.content = ""; perChat[index] = new PersonalChat(preChatsenData); perChat[index].Show(); aForm = new ArraysForm(perChat[index], preChatsenData.fromIP); arryFrom.Add(aForm); index++; } /// /// Enter发送消息 /// /// /// private void textBox2_KeyDown(object sender, KeyEventArgs e) { //if (e.KeyData == (Keys.Enter | Keys.Control)) //{ // textBox2.AppendText("\r\n"); //} if(e.KeyData==Keys.Enter) { Sees(); } } /// /// 发送完消息清空textBox /// /// /// private void textBox2_KeyUp(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Enter) { textBox2.Clear(); } } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: