UDP网络会议室视频已经录制好,这里贴上代码仅供参考
MainWindow代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets; namespace NetWork
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
IPAddress ip;
public MainWindow()
{ IPAddress [] ips=Dns .GetHostAddresses (Dns.GetHostName());
foreach (var v in ips )
{
if ( v .AddressFamily ==AddressFamily.InterNetwork )
{
ip = v;
}
}
InitializeComponent();
} private void button1_Click(object sender, RoutedEventArgs e)
{ Client c1 = new Client();
c1.Title = "客户端1";
c1.Local = new IPEndPoint(ip, );
c1.username = "张三";
c1.Show(); Client c2 = new Client();
c2.Title = "客户端2";
c2.Local = new IPEndPoint(ip, );
c2.username = "李四";
c2.Show(); Client c3 = new Client();
c3.Title = "客户端3";
c3.Local = new IPEndPoint(ip, );
c3.username = "王五";
c3.Show();
}
}
}
Client代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Threading; namespace NetWork
{
/// <summary>
/// Client.xaml 的交互逻辑
/// </summary>
public partial class Client : Window
{
public IPEndPoint Local ;
public IPEndPoint Remote=null;
public UdpClient udpclient;
public string username {
get { return textBox1 .Text; }
set { textBox1.Text = value; }
}
private IPAddress multicastAddress = IPAddress.Parse("224.0.1.10");
private bool isExit = false;
public Client()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
button3.IsEnabled = false;
udpclient = new UdpClient(Local);
udpclient.JoinMulticastGroup(multicastAddress); }
public void ReceiveMessage( )
{
while (isExit ==false)
{
try
{
byte [] result = udpclient.Receive(ref Remote);
string message = Encoding.Unicode.GetString(result);
string[] array = message.Split(',');
string command = array[]; switch (command)
{
case "Login":
string nm = array[];
AddUser(nm );
break;
case "Logout":
RemoveUser(array [] );
break;
case "Say":
AddSay(array [] +":"+array []);
break;
default:
break;
}
}
catch
{
break;
}
}
} private void button3_Click(object sender, RoutedEventArgs e)
{
SendMessage("Say,"+username+","+textBox2 .Text );
} private void button1_Click(object sender, RoutedEventArgs e)
{ Thread t1 = new Thread(ReceiveMessage);
t1.Start();
SendMessage("Login," + username); button1.IsEnabled = false;
button3.IsEnabled = true; }
public void SendMessage(string message)
{
byte[] bytes = Encoding.Unicode.GetBytes(message); for (int i = ; i < ; i++)
{
udpclient.Send(bytes, bytes.Length, multicastAddress.ToString(), i);
} }
private void RemoveUser(string name)
{
Action act = delegate()
{
listBox1.Items.Remove(name); };
listBox1.Dispatcher.Invoke(act );
}
private void AddUser( string name1)
{
Action act = delegate()
{
listBox1.Items.Add(name1);
};
listBox1.Dispatcher.Invoke(act); }
private void AddSay(string message )
{
Action act = delegate()
{
textBlock1.Text += message+"\n";
};
textBlock1.Dispatcher.Invoke(act); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
isExit = true;
SendMessage("Logout,"+username);
udpclient.DropMulticastGroup(multicastAddress);
udpclient.Close(); } }
}
一个普通的本科大学生,平常的时候无所事事不学习,临近考试却拼命的补习.几天时间就能学半学期的知识,早干嘛去了.悲哀.