C# 建立UDP服务器并接收客户端数据

C# 建立UDP服务器并接收客户端数据

 分类:
 

 

 

 

服务器端对象类库:

 

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net.Sockets;  
  6. using System.Net;  
  7. using System.Threading;  
  8. using System.IO;  
  9. namespace UdpChatExample  
  10. {  
  11.     /// <summary>  
  12.     /// UDP服务器对象  
  13.     /// </summary>  
  14.     public class UDPServerClass  
  15.     {  
  16.         public delegate void MessageHandler(string Message);//定义委托事件  
  17.         public event MessageHandler MessageArrived;  
  18.         public UDPServerClass()  
  19.         {  
  20.             //获取本机可用IP地址  
  21.             IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());  
  22.             foreach (IPAddress ipa in ips)  
  23.             {  
  24.                 if (ipa.AddressFamily == AddressFamily.InterNetwork)  
  25.                 {  
  26.                     MyIPAddress = ipa;//获取本地IP地址  
  27.                     break;  
  28.                 }  
  29.             }  
  30.             Note_StringBuilder = new StringBuilder();  
  31.             PortName = 8080;  
  32.   
  33.         }  
  34.   
  35.         public UdpClient ReceiveUdpClient;  
  36.   
  37.         /// <summary>  
  38.         /// 侦听端口名称  
  39.         /// </summary>  
  40.         public int PortName;  
  41.   
  42.         /// <summary>  
  43.         /// 本地地址  
  44.         /// </summary>  
  45.         public IPEndPoint LocalIPEndPoint;  
  46.   
  47.         /// <summary>  
  48.         /// 日志记录  
  49.         /// </summary>  
  50.         public StringBuilder Note_StringBuilder;  
  51.         /// <summary>  
  52.         /// 本地IP地址  
  53.         /// </summary>  
  54.         public IPAddress MyIPAddress;  
  55.   
  56.         public void Thread_Listen()  
  57.         {  
  58.             //创建一个线程接收远程主机发来的信息  
  59.             Thread myThread = new Thread(ReceiveData);  
  60.             myThread.IsBackground = true;  
  61.             myThread.Start();  
  62.         }  
  63.         /// <summary>  
  64.         /// 接收数据  
  65.         /// </summary>  
  66.         private void ReceiveData()  
  67.         {  
  68.             IPEndPoint local = new IPEndPoint(MyIPAddress, PortName);  
  69.             ReceiveUdpClient = new UdpClient(local);  
  70.             IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);  
  71.             while (true)  
  72.             {  
  73.                 try  
  74.                 {  
  75.                     //关闭udpClient 时此句会产生异常  
  76.                     byte[] receiveBytes = ReceiveUdpClient.Receive(ref remote);  
  77.                     string receiveMessage = Encoding.Default.GetString(receiveBytes, 0, receiveBytes.Length);  
  78.                     //  receiveMessage = ASCIIEncoding.ASCII.GetString(receiveBytes, 0, receiveBytes.Length);  
  79.                     MessageArrived(string.Format("{0}来自{1}:{2}", DateTime.Now.ToString(), remote, receiveMessage));  
  80.                     //try  
  81.                     //{  
  82.                     //    Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");  
  83.                     //    ReceiveUdpClient.Send(sendBytes, sendBytes.Length, local);  
  84.                     //}  
  85.                     //catch (Exception e)  
  86.                     //{  
  87.                     //}  
  88.                     //break;  
  89.   
  90.                 }  
  91.                 catch  
  92.                 {  
  93.                     break;  
  94.                 }  
  95.             }  
  96.         }  
  97.   
  98.         /// <summary>  
  99.         /// 添加日志信息到Note_StringBuilder  
  100.         /// </summary>  
  101.         public void AddMessage_Note_StringBuilder()  
  102.         {  
  103.   
  104.         }  
  105.     }  
  106. }  


 

界面简单代码:

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace UdpChatExample  
  11. {  
  12.     public partial class UDPServerForm : Form  
  13.     {  
  14.         public UDPServerForm()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.         public delegate void DelegateChangeText(string Messages);  
  19.         void ChangeTxt(string Messages)  
  20.         {  
  21.             string SBText = SB.ToString();  
  22.             SB.Remove(0, SB.Length);  
  23.             SB.Append(Messages + "\r\n" + SBText);  
  24.             richTextBox1.Text = SB.ToString();  
  25.         }  
  26.         StringBuilder SB = new StringBuilder();  
  27.         private void UDPServerForm_Load(object sender, EventArgs e)  
  28.         {  
  29.   
  30.         }  
  31.   
  32.         UDPServerClass UDPServerClass1;  
  33.         private void button1_Click(object sender, EventArgs e)  
  34.         {  
  35.             UDPServerClass1 = new UDPServerClass();  
  36.             UDPServerClass1.Thread_Listen();  
  37.             UDPServerClass1.MessageArrived += new UDPServerClass.MessageHandler(UDPServerClass1_MessageArrived);  
  38.         }  
  39.   
  40.         void UDPServerClass1_MessageArrived(string Message)  
  41.         {  
  42.             richTextBox1 .Invoke (new DelegateChangeText(ChangeTxt),Message);  
  43.         }   
  44.   
  45.         private void UDPServerForm_FormClosing(object sender, FormClosingEventArgs e)  
  46.         {  
  47.             System.Environment.Exit(System.Environment.ExitCode);  
  48.         }  
  49.     }  
  50. }  


 

 

 

posted on 2016-04-29 14:27  和轩僮  阅读(4391)  评论(0编辑  收藏  举报

导航