程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#使用UDP Client編寫聊天程序(2)

C#使用UDP Client編寫聊天程序(2)

編輯:關於C語言

下面的示例使用 UdpClIEnt 偵聽端口8080 上的多路廣播地址組 10.0.0.1 的 UDP 數據文報廣播。它接收消息字符串並將消息寫入控制台。

例:在IDE新建一個工程項目,窗體類命名為FormChat,在窗體上放一個控件ListBox,用於接收消息,放一個TextBox控件用於輸入要發送的消息,放一個Button用於發送消息。全源代碼如下:

using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Net;  
using System.Net.Sockets;  
using System.Threading;  
using System.Text;  
namespace WinUDPChat 
{ 
 ///  
 /// Form1 的摘要說明。 
 ///  
 public class FormChat : System.Windows.Forms.Form 
 { 
  ///  
  /// 必需的設計器變量。 
  ///  
  private System.ComponentModel.Container components = null; 
  private static UdpClient m_Client;  
  //private static UdpClIEnt m_Server;  
  
  private static int ListenerPort = 8080;  
  private static int SenderPort = 8080;  
  private static int LocalPort;  
  private static int RemotePort;  
  
  private static string m_szHostName;  
  
  private static IPAddress m_GroupAddress_C;  
  private static IPAddress m_GroupAddress_S;  
  private static IPHostEntry m_LocalHost;  
  private static IPEndPoint m_RemoteEP;  
  
  private System.Windows.Forms.Button button_sendMSG; 
  private System.Windows.Forms.TextBox textBox_msg; 
  private System.Windows.Forms.ListBox listBox_msg;  
  private Thread th; 
  public FormChat() 
  { 
   // 
   // Windows 窗體設計器支持所必需的 
   // 
   InitializeComponent(); 
   // 
   // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼 
   // 
  } 
  ///  
  /// 清理所有正在使用的資源。 
  ///  
  protected override void Dispose( bool disposing ) 
  { 
   if( disposing ) 
   { 
    if (components != null)  
    { 
     components.Dispose(); 
    } 
   } 
   base.Dispose( disposing ); 
  } 
  #region Windows 窗體設計器生成的代碼 
  ///  
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 
  /// 此方法的內容。 
  ///  
  private void InitializeComponent() 
  { 
   this.button_sendMSG = new System.Windows.Forms.Button(); 
   this.textBox_msg = new System.Windows.Forms.TextBox(); 
   this.listBox_msg = new System.Windows.Forms.ListBox(); 
   this.SuspendLayout(); 
   //  
   // button_sendMSG 
   //  
   this.button_sendMSG.Location = new System.Drawing.Point(96, 234); 
   this.button_sendMSG.Name = "button_sendMSG"; 
   this.button_sendMSG.TabIndex = 0; 
   this.button_sendMSG.Text = "發送"; 
   this.button_sendMSG.Click += new System.EventHandler(this.button_sendMSG_Click); 
   //  
   // textBox_msg 
   //  
   this.textBox_msg.Location = new System.Drawing.Point(80, 197); 
   this.textBox_msg.Name = "textBox_msg"; 
   this.textBox_msg.TabIndex = 1; 
   this.textBox_msg.Text = ""; 
   //  
   // listBox_msg 
   //  
   this.listBox_msg.ItemHeight = 12; 
   this.listBox_msg.Location = new System.Drawing.Point(21, 22); 
   this.listBox_msg.Name = "listBox_msg"; 
   this.listBox_msg.Size = new System.Drawing.Size(248, 136); 
   this.listBox_msg.TabIndex = 2; 
   //  
   // FormChat 
   //  
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
   this.ClIEntSize = new System.Drawing.Size(292, 273); 
   this.Controls.Add(this.listBox_msg); 
   this.Controls.Add(this.textBox_msg); 
   this.Controls.Add(this.button_sendMSG); 
   this.Name = "FormChat"; 
   this.Text = "聊天室"; 
   this.Load += new System.EventHandler(this.FormChat_Load); 
   this.Closed += new System.EventHandler(this.FormChat_Closed); 
   this.ResumeLayout(false); 
  } 
  #endregion 
  ///  
  /// 應用程序的主入口點。 
  ///  
  [STAThread] 
  static void Main()  
  { 
   Application.Run(new FormChat()); 
  } 
  private void FormChat_Load(object sender, System.EventArgs e) 
  { 
   LocalPort = SenderPort;  
   RemotePort = ListenerPort;  
   m_szHostName = Dns.GetHostName();  
  
   m_LocalHost = Dns.GetHostByName(m_szHostName);  
   
   Initialize();  
   
   th = new Thread(new ThreadStart(Listener));  
   th.Start();  
  
  } 
  public void Terminate()  
  {  
   m_Client.DropMulticastGroup(m_GroupAddress_C);  
  }  
  public void Initialize()  
  {  
  
   //  
   // 實例化 UdpCLient  
   //  
   m_Client = new UdpClIEnt(LocalPort);  
  
   //  
   // 創建對方主機的終結點  
   //  
   m_GroupAddress_S = IPAddress.Parse("10.0.0.1"); //要發送到的計算機IP 
   m_RemoteEP = new IPEndPoint( m_GroupAddress_S, RemotePort );  
  
  }  
  public void Listener()  
  {  
   //  
   // 創建多路廣播組對象  
   //  
   System.Net.IPHostEntry localhost =Dns.GetHostByName(Dns.GetHostName()); 
   string local_IP= localhost.AddressList[0].ToString();//接收消息的本地IP,用於監聽 
   m_GroupAddress_C = IPAddress.Parse(local_IP);  
  
   //  
   // 聯接組  
   //  
   try  
   {  
    m_Client.JoinMulticastGroup(m_GroupAddress_C, 100); //不添加到多路廣播組也可以 
   }  
   catch(Exception err)  
   {  
    throw(err);//無法聯接多路廣播組 
     
   }  
  
   //  
   // 偵聽器等待數據到來  
   // 並用緩沖區保存它。  
  
   Thread.Sleep(2000); // 確保 client2 正在接收  
  
   Encoding  ASCII = Encoding.ASCII;  
  
//   while(!m_Done)  
   while(true)  
   {  
    IPEndPoint endpoint = null;  
    //endpoint = new IPEndPoint(m_GroupAddress_C,LocalPort);//這句代碼不要也可以 
    Byte[] data = m_Client.Receive(ref endpoint);  
  
    String strData = ASCII.GetString(data);  
     listBox_msg.Items.Add(strData); 
   }  
  
  } 
  private void FormChat_Closed(object sender, System.EventArgs e) 
  { 
   try 
   { 
    try 
    { 
     if(th!=null) 
     { 
      if(th.IsAlive) 
      { 
       th.Abort(); 
       
      } 
      
      th=null; 
      
     } 
     
    } 
    catch 
    { 
     try 
     { 
      System.Threading.Thread.ResetAbort(); 
     } 
     catch 
     {} 
    } 
   
    Terminate();  
    m_Client.Close(); 
   } 
   catch 
   { 
   } 
  } 
  private void button_sendMSG_Click(object sender, System.EventArgs e) 
  { 
   Byte [] buffer = null;  
  
   Encoding ASCII = Encoding.ASCII;  

    string s = textBox_msg.Text; 
  
    buffer = new Byte[s.Length + 1];  
    //  
    // 將數據發送給遠程對方主機  
    //  
  
    int len = ASCII.GetBytes( s.ToCharArray(), 0, s.Length, buffer, 0);  
  
    int ecode = m_ClIEnt.Send(buffer, len, m_RemoteEP);  
  
   
  }  
 } 
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved