码迷,mamicode.com
首页 > 其他好文 > 详细

UDP网路会议室的代码

时间:2016-06-26 21:14:10      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

 

  UDP网络会议室视频已经录制好,这里贴上代码仅供参考

  MainWindow代码:

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using System.Net;
15 using System.Net.Sockets;
16 
17 namespace NetWork
18 {
19     /// <summary>
20     /// MainWindow.xaml 的交互逻辑
21     /// </summary>
22     public partial class MainWindow : Window
23     {
24         IPAddress ip;
25         public MainWindow()
26         {
27            
28              IPAddress [] ips=Dns .GetHostAddresses (Dns.GetHostName());
29         foreach (var  v in ips )
30        {
31             if ( v .AddressFamily ==AddressFamily.InterNetwork )
32             {
33                 ip = v;
34             }
35         }
36             InitializeComponent();
37         }
38 
39         private void button1_Click(object sender, RoutedEventArgs e)
40         {
41 
42             Client c1 = new Client();
43             c1.Title = "客户端1";
44             c1.Local = new IPEndPoint(ip, 8001);
45             c1.username = "张三";
46             c1.Show();
47 
48             Client c2 = new Client();
49             c2.Title = "客户端2";
50             c2.Local = new IPEndPoint(ip, 8002);
51             c2.username = "李四";
52             c2.Show();
53 
54             Client c3 = new Client();
55             c3.Title = "客户端3";
56             c3.Local = new IPEndPoint(ip, 8003);
57             c3.username = "王五";
58             c3.Show();
59         }
60     }
61 }

 

  Client代码:

  

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Windows;
  6 using System.Windows.Controls;
  7 using System.Windows.Data;
  8 using System.Windows.Documents;
  9 using System.Windows.Input;
 10 using System.Windows.Media;
 11 using System.Windows.Media.Imaging;
 12 using System.Windows.Shapes;
 13 using System.Net;
 14 using System.Net.Sockets;
 15 using System.Threading;
 16 
 17 namespace NetWork
 18 {
 19     /// <summary>
 20     /// Client.xaml 的交互逻辑
 21     /// </summary>
 22     public partial class Client : Window
 23     {
 24         public IPEndPoint Local  ;
 25         public IPEndPoint Remote=null;   
 26         public UdpClient udpclient;
 27         public string username {
 28             get { return textBox1 .Text; }
 29             set { textBox1.Text = value; }
 30         }
 31         private IPAddress multicastAddress = IPAddress.Parse("224.0.1.10");
 32         private bool isExit = false;
 33         public Client()
 34         {
 35             InitializeComponent();
 36         }
 37         private void Window_Loaded(object sender, RoutedEventArgs e)
 38         {
 39             button3.IsEnabled = false;
 40             udpclient = new UdpClient(Local);
 41             udpclient.JoinMulticastGroup(multicastAddress);
 42            
 43         }
 44         public void ReceiveMessage( ) 
 45         {
 46             while (isExit ==false)
 47             {
 48                 try
 49                 {
 50                     byte [] result = udpclient.Receive(ref Remote);
 51                     string message = Encoding.Unicode.GetString(result);           
 52                     string[] array = message.Split(,);
 53                     string command = array[0];
 54                     
 55                     switch (command)
 56                     {
 57                         case "Login":
 58                             string nm = array[1];
 59                             AddUser(nm );
 60                             break;
 61                         case "Logout":
 62                             RemoveUser(array [1] );
 63                             break;
 64                         case "Say":
 65                             AddSay(array [1] +":"+array [2]);
 66                             break;
 67                         default:
 68                             break;
 69                     }
 70                 }
 71                 catch 
 72                 {
 73                     break;
 74                 }
 75             }
 76         }
 77 
 78         private void button3_Click(object sender, RoutedEventArgs e)
 79         {
 80             SendMessage("Say,"+username+","+textBox2 .Text );
 81         }
 82 
 83         private void button1_Click(object sender, RoutedEventArgs e)
 84         {
 85 
 86             Thread t1 = new Thread(ReceiveMessage);
 87             t1.Start();
 88             SendMessage("Login," + username);
 89             
 90             button1.IsEnabled = false;
 91             button3.IsEnabled = true;
 92 
 93         }
 94         public void SendMessage(string message)
 95         {
 96             byte[] bytes = Encoding.Unicode.GetBytes(message);
 97    
 98             for (int i = 8001; i < 8004; i++)
 99             {
100                 udpclient.Send(bytes, bytes.Length, multicastAddress.ToString(), i);
101             }    
102            
103         }
104         private void RemoveUser(string name) 
105         {
106             Action act = delegate()
107             {
108                 listBox1.Items.Remove(name);
109                 
110             };
111             listBox1.Dispatcher.Invoke(act );
112         }
113         private void AddUser( string name1) 
114         {
115             Action act = delegate()
116            {
117                 listBox1.Items.Add(name1);
118             };
119             listBox1.Dispatcher.Invoke(act);
120             
121         }
122         private void AddSay(string message ) 
123         {
124             Action act = delegate() 
125             {
126                 textBlock1.Text += message+"\n";
127             };
128             textBlock1.Dispatcher.Invoke(act);
129             
130         }
131 
132         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
133         {
134             isExit = true;
135             SendMessage("Logout,"+username);
136             udpclient.DropMulticastGroup(multicastAddress);        
137             udpclient.Close();       
138            
139         }
140            
141     }
142 }

 

  一个普通的本科大学生,平常的时候无所事事不学习,临近考试却拼命的补习.几天时间就能学半学期的知识,早干嘛去了.悲哀.

  

  

UDP网路会议室的代码

标签:

原文地址:http://www.cnblogs.com/yunquan/p/5618505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!