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 UDP
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1 ;
private System.Windows.Forms.ListBox listBox1 ;
private System.Windows.Forms.TextBox textBox1 ;
private System.Windows.Forms.Label label2 ;
private System.Windows.Forms.Button button1 ;
private System.Windows.Forms.Button button2 ;
private System.Windows.Forms.TextBox textBox2 ;
private System.Windows.Forms.Label label3 ;
private System.Windows.Forms.StatusBar statusBar1 ;
private bool ReadFlag = true ;
//設定偵聽標示位,通過它來設定是否偵聽連接埠號碼
private Thread th ;
//定義一個線程,線上程接收聊天資訊
private IPEndPoint remote ;
//定義一個遠程結點,用以擷取遠端電腦IP地址和發送的聊天資訊
private UdpClient udpclient ;
//建立一個UDP網路服務
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent ( ) ;
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose ( bool disposing )
{
/*try
{
ReadFlag = false ;
th.Abort ( ) ;
remote =null ;
udpclient.Close ( ) ;
th = null ;
}
catch
{}*/
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
#region Windows Form Designer generated code
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent ( )
{
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(11, 6);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 23);
this.label1.TabIndex = 0;
this.label1.Text = "接收資訊:";
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(9, 28);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(318, 121);
this.listBox1.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(72, 200);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(183, 20);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(26, 203);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(91, 23);
this.label2.TabIndex = 4;
this.label2.Text = "資訊:";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(272, 200);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(42, 28);
this.button1.TabIndex = 5;
this.button1.Text = "發送";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(272, 160);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(42, 28);
this.button2.TabIndex = 6;
this.button2.Text = "接收";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(73, 163);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(181, 20);
this.textBox2.TabIndex = 8;
this.textBox2.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(14, 164);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(52, 23);
this.label3.TabIndex = 7;
this.label3.Text = "IP地址:";
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 251);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(337, 22);
this.statusBar1.TabIndex = 9;
this.statusBar1.Text = "程式不處於資料接收狀態";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(300, 280);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar1,
this.textBox2,
this.label3,
this.button2,
this.button1,
this.textBox1,
this.listBox1,
this.label2,
this.label1});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "UDP聊天";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void read ( )
{
//偵聽本地的8000連接埠號碼
udpclient = new UdpClient ( 8000 ) ;
remote = null ;
//設定編碼類別型
Encoding enc = Encoding.Unicode ;
while ( ReadFlag == true )
{
Byte[] data = udpclient.Receive ( ref remote ) ;
//得到對方發送來的聊天資訊
String strData = enc.GetString ( data ) ;
//擷取傳送資訊到本地連接埠號碼的遠端電腦IP地址
string remoteIP = remote.Address.ToString ( ) ;
//顯示接收資訊以及傳送資訊的電腦IP地址
listBox1.Items.Add ( remoteIP + ":" + strData ) ;
}
}
private void button1_Click ( object sender , System.EventArgs e )
{
//建立UDP網路服務
UdpClient SendUdp = new UdpClient ( ) ;
IPAddress remoteIP ;
//判斷IP地址的正確性
try
{
remoteIP = IPAddress.Parse ( textBox2.Text );
}
catch
{
MessageBox.Show ( "請輸入正確的IP地址!" , "錯誤" ) ;
return ;
}
IPEndPoint remoteep = new IPEndPoint ( remoteIP , 8000 ) ;
Byte [] buffer = null ;
Encoding enc = Encoding.Unicode ;
string str = textBox1.Text ;
buffer = enc.GetBytes ( str.ToCharArray ( ) ) ;
//傳送資訊到指定電腦的8000連接埠號碼
SendUdp.Send ( buffer , buffer.Length , remoteep ) ;
textBox1.Clear ( ) ;
//關閉UDP網路服務
SendUdp.Close ( ) ;
}
private void button2_Click ( object sender , System.EventArgs e )
{
//建立一個線程
th = new Thread ( new ThreadStart ( read ) ) ;
//啟動線程
th.Start ( ) ;
statusBar1.Text ="程式處於資料接收狀態!" ;
}
}
}