首先登陆是FORM2,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace UDP
{
public partial class Form2 : Form
{
public static string strUserName;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
strUserName = this.textBox1.Text;
if (this.textBox1.Text!="")
{
Form1 f1 = new Form1();
f1.Show();
this.Hide();
this.Hide();
this.ShowInTaskbar = false;
}
else
MessageBox.Show("用户名不能为空!");
}
}
}
下面是聊天窗口(FORM1)的代码:
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 UDP
{
public partial class Form1 : Form
{
private UdpClient client = new UdpClient(8888);
private bool IsContinue = true;
//public static string strUserName = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
this.textBox2.Text = Form2.strUserName; //类名打点 form2是个类阿 .........汗.........
Thread myThread = new Thread(new ThreadStart(StartReceive));
myThread.Start();
}
private void StartReceive()
{
IPAddress groupIP = IPAddress.Parse("224.1.6.8");
try
{
client.JoinMulticastGroup(groupIP); //将本机加入广播组
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
//*****************一启动程序就通知所有在线主机自己上线了********************
string strSend = "START " + Form2.strUserName;
Send(strSend);
//******************等待接受消息*************************************
while (IsContinue)
{
IPEndPoint myIPEndPoint=new IPEndPoint(IPAddress.Parse("127.0.0.1"),8888);
byte[] data = client.Receive(ref myIPEndPoint);//将接受主机设置为自己的主机,端口设置为8888
string strData = System.Text.Encoding.Default.GetString(data);
string[] str = strData.Split(' ');//注意要用单引号
if (str[0] == "START")
{
if (str[1] != Form2.strUserName) //如果不是自己则加入在线列表
{
this.listBox1.Items.Add(str[1]);
strSend = "JOIN " + Form2.strUserName;
Send(strSend);
}
}
else if (str[0] == "JOIN")
{
if (this.listBox1.Items.Contains(str[1]) == false) //如果不包含则添加
{
this.listBox1.Items.Add(str[1]);
}
}
else if (str[0] =="PRIV")
{
if (str[1] == Form2.strUserName) //私聊,如果是接受的人接到消息则显示
{
this.richTextBox1.Text = str[2].ToString() + "/r/n";
}
//else
// MessageBox.Show("no"); //不是给你的
}
else //如果不是私聊,则所有人都显示
{
this.richTextBox1.Text = strData.ToString() + "/r/n";
}
}
}
private void Send(string strSend)
{
byte[] data=System.Text.Encoding.Default.GetBytes(strSend);
UdpClient myClient = new UdpClient();
IPEndPoint groupIPEndPoint=new IPEndPoint(IPAddress.Parse("224.1.6.8"),8888);
myClient.Send(data, data.Length, groupIPEndPoint);
}
private void button1_Click(object sender, EventArgs e)
{//发送消息
if (this.listBox1.Items.Count ==0)
MessageBox.Show("当前没人在线!");
else //如果有人在线
{
string strSend = this.richTextBox2.Text;
if (this.checkBox1.Checked == true) //如果私聊
{
if (this.listBox1.SelectedItems.Count != 1) //如果没选中任何联系人不为一个
{
MessageBox.Show("请先选择要私聊的一个联系人!");
return;
}
else
{
strSend = "PRIV " + this.listBox1.SelectedItem.ToString()+" " + this.richTextBox2.Text;
}
}
else //如果群聊
{}
byte[] data = System.Text.Encoding.Default.GetBytes(strSend);
UdpClient myClient = new UdpClient();
IPEndPoint myIPEndPoint = new IPEndPoint(IPAddress.Parse("224.1.6.8"), 8888);
myClient.Send(data, data.Length, myIPEndPoint);
}
this.richTextBox2.Text = "";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(Environment.ExitCode);
}
}
}注意:程序中存在很多bug,例如分割字符串是按空格来分的(极其不合理,可改成不常用的如start*|*username,到时候按*|*来分割)导致发送的文字中如果有空格,空格以后的文字将会丢失...
还有在线名单重复添加的问题:应该在listbox的项的数目改变时写:
if (this.listBox1.Items.Contains(str[1]) == false) //如果不包含则添加
{
this.listBox1.Items.Add(str[1]);
}但会导致两个人同名的只能加一个上去,可以用ip地址来区分解决....