namespace UDP_Broadcast { partial class Form1 { /// <summary> /// 設計工具所需的變數。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清除任何使用中的資源。 /// </summary> /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form 設計工具產生的程式碼 /// <summary> /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改 /// 這個方法的內容。 /// </summary> private void InitializeComponent() { this.buttonSend = new System.Windows.Forms.Button(); this.buttonReceive = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // buttonSend // this.buttonSend.Location = new System.Drawing.Point(12, 40); this.buttonSend.Name = "buttonSend"; this.buttonSend.Size = new System.Drawing.Size(75, 23); this.buttonSend.TabIndex = 0; this.buttonSend.Text = "Send"; this.buttonSend.UseVisualStyleBackColor = true; this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click); // // buttonReceive // this.buttonReceive.Location = new System.Drawing.Point(93, 40); this.buttonReceive.Name = "buttonReceive"; this.buttonReceive.Size = new System.Drawing.Size(75, 23); this.buttonReceive.TabIndex = 1; this.buttonReceive.Text = "Receive"; this.buttonReceive.UseVisualStyleBackColor = true; this.buttonReceive.Click += new System.EventHandler(this.buttonReceive_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(75, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(211, 22); this.textBox1.TabIndex = 2; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(14, 15); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(55, 12); this.label1.TabIndex = 3; this.label1.Text = "Send Text:"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(298, 77); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Controls.Add(this.buttonReceive); this.Controls.Add(this.buttonSend); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.Text = "Form1"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button buttonSend; private System.Windows.Forms.Button buttonReceive; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; } }
Code:
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Windows.Forms; namespace UDP_Broadcast { public partial class Form1 : Form { /// <summary> /// Port /// </summary> private const int PORT = 12345; /// <summary> /// 訊息 /// </summary> private String msg { get { return String.IsNullOrEmpty(textBox1.Text) ? "Hi, C.Y. Fang" : textBox1.Text; } } /// <summary> /// 接收 UDP /// </summary> private readonly UdpClient receive = new UdpClient(PORT); public Form1() { InitializeComponent(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { receive.Close(); } /// <summary> /// 送出訊息的按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonSend_Click(object sender, EventArgs e) { SendMessage(); } /// <summary> /// 接收廣播的按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonReceive_Click(object sender, EventArgs e) { ReceiveBroadcast(); } private void SendMessage() { //UDP UdpClient client = new UdpClient(); //Address IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, PORT); //Message byte[] data = Encoding.UTF8.GetBytes(msg); //Send Message client.Send(data, data.Length, ip); client.Close(); } private void ReceiveBroadcast() { Console.WriteLine("Server started"); try { //開始接收 receive.BeginReceive(ReceiveCallBack, null); } catch (SocketException ex) { Console.WriteLine(String.Format("ErrorCode:{0}, ErrorMessage:{1}", ex.ErrorCode, ex.Message)); } } private void ReceiveCallBack(IAsyncResult ar) { //IP IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT); //收資料 byte[] data = receive.EndReceive(ar, ref ip); //UTF8編碼 String text = Encoding.UTF8.GetString(data); //印出接收到的訊息 Console.WriteLine(String.Format("IP Address{0}, Message:{1}", ip.Address, text)); //持續接收 ReceiveBroadcast(); } } }
執行結果: