【文件属性】:
文件名称:仿飞秋,局域网聊天工具
文件大小:80KB
文件格式:ZIP
更新时间:2016-01-24 09:54:17
飞秋 局域网聊天 网络编程
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();
}
}
}
}
【文件预览】:
TcpIpTest多人聊天
----WindowsFormsApplication2()
--------bin()
--------WindowsFormsApplication2.csproj(6KB)
--------Form1.Designer.cs(6KB)
--------Program.cs(485B)
--------obj()
--------WindowsFormsApplication2.csproj.user(545B)
--------Class2.cs(2KB)
--------Class1.cs(2KB)
--------Form1.cs(10KB)
--------app.config(120B)
--------PersonalChat.Designer.cs(4KB)
--------WindowsFormsApplication2_TemporaryKey.pfx(2KB)
--------Form1.resx(6KB)
--------PersonalChat.resx(6KB)
--------PersonalChat.cs(2KB)
--------Properties()
----TcpIpTest.suo(50KB)
----TcpIpTest.sln(962B)