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 NetMeetingExample
{
public partial class Form1 : Form
{
///
/// 列表框操作选项枚举
///
private enum ListBoxOperation
{
AddItem,
RemoveItem
}
///
/// 定义列表框委托
///
///
///
///
delegate void SetListBoxItemCallBack(ListBox listbox, string text, ListBoxOperation operation);
///
/// 定义委托
///
SetListBoxItemCallBack setlistboxitemcallback;
///
/// 使用的IP地址,组播地址的范围 224.0.0.0-239.255.255.255
///
private IPAddress ip = IPAddress.Parse("224.224.125.119");
//使用的接收端口
private int port = 51008;
///
/// 定义udp
///
private UdpClient udpclient;
public Form1()
{
InitializeComponent();
setlistboxitemcallback = new SetListBoxItemCallBack(SetListItem);
}
///
/// 委托处理过程
///
///
///
///
private void SetListItem(ListBox listbox, string text, ListBoxOperation operation)
{
if (listbox.InvokeRequired == true)
{
this.Invoke(setlistboxitemcallback, listbox, text, operation);
}
else
{
if (operation == ListBoxOperation.AddItem)
{
if (listbox == LBAddress)
{
if (listbox.Items.Contains(text) == false)
{
listbox.Items.Add(text);
}
}
else
{
listbox.Items.Add(text);
}
listbox.SelectedIndex = listbox.Items.Count - 1;
}
else if (operation == ListBoxOperation.RemoveItem)
{
listbox.Items.Remove(text);
}
}
}
///
/// 向所有成员发送信息
///
/// 发送的信息内容
private void SendToAll(string sendString)
{
UdpClient myudpclient = new UdpClient();
//允许发送或接收广播数据报
myudpclient.EnableBroadcast = true;
//必须使用组播地址范围内的地址
IPEndPoint iep = new IPEndPoint(ip, port);
//将发送内容转换成字节数组
byte[] bytes = Encoding.UTF8.GetBytes(sendString);
try
{
//向子网发送信息
myudpclient.Send(bytes,bytes.Length, iep);
}
catch (Exception err)
{
MessageBox.Show(err.Message,"发送失败");
}
finally
{
myudpclient.Close();
}
}
///
/// 接收线程
///
private void RecMsg()
{
udpclient = new UdpClient(port);
//必须使用组播地址内的地址
udpclient.JoinMulticastGroup(ip);
udpclient.Ttl = 50;
IPEndPoint remote = null;
while (true)
{
try
{
//关闭udpclient 时此句会产生异常
byte[] bytes = udpclient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
string[] splitstring = str.Split(',');
int s = splitstring[0].Length;
switch (splitstring[0])
{
case "Login"://进入会议室
SetListItem(LBMsg, string.Format("[{0}]进入会议室", remote.Address.ToString()), ListBoxOperation.AddItem);
string userListString = "List," + remote.Address.ToString();
for (int i = 0; i < LBAddress.Items.Count; i++)
{
userListString += "," + LBAddress.Items[i].ToString();
}
SendToAll(userListString);
break;
case "List"://成员名单
for (int i = 1; i < splitstring.Length; i++)
{
SetListItem(LBAddress, splitstring[i], ListBoxOperation.AddItem);
}
break;
case "Message"://发送内容
SetListItem(LBMsg,string.Format("[{0}]说:{1}",remote.Address,str.Substring(8)),ListBoxOperation.AddItem);
break;
case "Logout"://退出
SetListItem(LBMsg, string.Format("[{0}]退出", remote.Address), ListBoxOperation.AddItem);
SetListItem(LBAddress, remote.Address.ToString(), ListBoxOperation.RemoveItem);
break;
}
}
catch (Exception err)
{
//退出
break;
}
finally
{
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (TBMsg.Text.Trim().Length > 0)
{
SendToAll("Message," + TBMsg.Text);
TBMsg.Clear();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Btn_logout.Enabled == true)
{
MessageBox.Show("请先退出会议室再关闭");
e.Cancel = true;
}
}
private void Btn_logout_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
SendToAll("Logout");
Thread.Sleep(1000);
udpclient.Close();
Btn_login.Enabled = true;
Btn_logout.Enabled = false;
Cursor.Current = Cursors.Default;
}
private void Btn_login_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
Thread mythread = new Thread(new ThreadStart(RecMsg));
mythread.Start();
Thread.Sleep(1000);
SendToAll("Login");
Btn_login.Enabled = false;
Btn_logout.Enabled = true;
Cursor.Current = Cursors.Default;
}
private void Form1_Load(object sender, EventArgs e)
{
Btn_login.Enabled = true;
Btn_logout.Enabled = false;
}
}
}
0 评论:
发表评论