C# UDP通信

如下是界面布局代码


namespace
SmartHomeMsgTest { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.txtMsg = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.btnLogin = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.checkBox4 = new System.Windows.Forms.CheckBox(); this.checkBox5 = new System.Windows.Forms.CheckBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.txtServerIP = new System.Windows.Forms.TextBox(); this.btnInit = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // txtMsg // this.txtMsg.Dock = System.Windows.Forms.DockStyle.Bottom; this.txtMsg.Location = new System.Drawing.Point(0, 304); this.txtMsg.Multiline = true; this.txtMsg.Name = "txtMsg"; this.txtMsg.Size = new System.Drawing.Size(786, 248); this.txtMsg.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(6, 153); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 23); this.button1.TabIndex = 1; this.button1.Text = "发送订阅数据"; this.button1.UseVisualStyleBackColor = true; // // btnLogin // this.btnLogin.Location = new System.Drawing.Point(181, 66); this.btnLogin.Name = "btnLogin"; this.btnLogin.Size = new System.Drawing.Size(75, 23); this.btnLogin.TabIndex = 2; this.btnLogin.Text = "发送登录"; this.btnLogin.UseVisualStyleBackColor = true; this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click); // // button3 // this.button3.Location = new System.Drawing.Point(6, 191); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(120, 23); this.button3.TabIndex = 1; this.button3.Text = "取消订阅数据"; this.button3.UseVisualStyleBackColor = true; // // checkBox1 // this.checkBox1.AutoSize = true; this.checkBox1.Location = new System.Drawing.Point(6, 22); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(48, 16); this.checkBox1.TabIndex = 3; this.checkBox1.Text = "报警"; this.checkBox1.UseVisualStyleBackColor = true; // // checkBox2 // this.checkBox2.AutoSize = true; this.checkBox2.Location = new System.Drawing.Point(6, 44); this.checkBox2.Name = "checkBox2"; this.checkBox2.Size = new System.Drawing.Size(60, 16); this.checkBox2.TabIndex = 3; this.checkBox2.Text = "布撤防"; this.checkBox2.UseVisualStyleBackColor = true; // // checkBox3 // this.checkBox3.AutoSize = true; this.checkBox3.Location = new System.Drawing.Point(6, 66); this.checkBox3.Name = "checkBox3"; this.checkBox3.Size = new System.Drawing.Size(48, 16); this.checkBox3.TabIndex = 3; this.checkBox3.Text = "空调"; this.checkBox3.UseVisualStyleBackColor = true; // // checkBox4 // this.checkBox4.AutoSize = true; this.checkBox4.Location = new System.Drawing.Point(6, 88); this.checkBox4.Name = "checkBox4"; this.checkBox4.Size = new System.Drawing.Size(48, 16); this.checkBox4.TabIndex = 3; this.checkBox4.Text = "新风"; this.checkBox4.UseVisualStyleBackColor = true; // // checkBox5 // this.checkBox5.AutoSize = true; this.checkBox5.Location = new System.Drawing.Point(6, 110); this.checkBox5.Name = "checkBox5"; this.checkBox5.Size = new System.Drawing.Size(48, 16); this.checkBox5.TabIndex = 3; this.checkBox5.Text = "地暖"; this.checkBox5.UseVisualStyleBackColor = true; // // groupBox1 // this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.checkBox5); this.groupBox1.Controls.Add(this.button3); this.groupBox1.Controls.Add(this.checkBox4); this.groupBox1.Controls.Add(this.checkBox1); this.groupBox1.Controls.Add(this.checkBox3); this.groupBox1.Controls.Add(this.checkBox2); this.groupBox1.Location = new System.Drawing.Point(25, 51); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(143, 233); this.groupBox1.TabIndex = 4; this.groupBox1.TabStop = false; this.groupBox1.Text = "手机推送订阅"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(229, 19); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 21); this.textBox2.TabIndex = 5; this.textBox2.Text = "15017941380"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(376, 19); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(100, 21); this.textBox3.TabIndex = 6; this.textBox3.Text = "ABCabc123"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(179, 23); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 7; this.label1.Text = "手机号"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(338, 23); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(29, 12); this.label2.TabIndex = 7; this.label2.Text = "密码"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(0, 23); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(65, 12); this.label3.TabIndex = 8; this.label3.Text = "服务器地址"; // // txtServerIP // this.txtServerIP.Location = new System.Drawing.Point(68, 19); this.txtServerIP.Name = "txtServerIP"; this.txtServerIP.Size = new System.Drawing.Size(100, 21); this.txtServerIP.TabIndex = 9; this.txtServerIP.Text = "120.79.68.201"; // // btnInit // this.btnInit.Location = new System.Drawing.Point(503, 16); this.btnInit.Name = "btnInit"; this.btnInit.Size = new System.Drawing.Size(75, 23); this.btnInit.TabIndex = 10; this.btnInit.Text = "初始化"; this.btnInit.UseVisualStyleBackColor = true; this.btnInit.Click += new System.EventHandler(this.btnInit_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(786, 552); this.Controls.Add(this.btnInit); this.Controls.Add(this.txtServerIP); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.groupBox1); this.Controls.Add(this.btnLogin); this.Controls.Add(this.txtMsg); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txtMsg; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button btnLogin; private System.Windows.Forms.Button button3; private System.Windows.Forms.CheckBox checkBox1; private System.Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.CheckBox checkBox4; private System.Windows.Forms.CheckBox checkBox5; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtServerIP; private System.Windows.Forms.Button btnInit; } }
如下是UPD初始化与通信实现代码

using
System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; namespace SmartHomeMsgTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 用于UDP发送的网络服务类 /// </summary> private UdpClient udpcSend; private int port = 60000; IPEndPoint remoteIpep = null; IPEndPoint localIpep = null; private void Form1_Load(object sender, EventArgs e) { localIpep = new IPEndPoint(IPAddress.Any, 10001); // 本机IP,指定的端口号 } private void btnInit_Click(object sender, EventArgs e) { if (udpcSend == null) { remoteIpep = new IPEndPoint(IPAddress.Parse(this.txtServerIP.Text.Trim()), port); // 发送到的IP地址和端口号 udpcSend = new UdpClient(localIpep); ThreadPool.QueueUserWorkItem((a) => { ReceiveMessage(); }); this.txtServerIP.Enabled = false; this.btnInit.Text = "反初始化"; } else { this.txtServerIP.Enabled = true; udpcSend.Close(); udpcSend = null; this.btnInit.Text = "初始化"; } } private void btnLogin_Click(object sender, EventArgs e) { string msg = "0123456789"; SendMessage(msg); ShowMsg(msg); } private void SendMessage(string message) { try { ThreadPool.QueueUserWorkItem((a) => { byte[] sendbytes = Encoding.UTF8.GetBytes(message); udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep); }); } catch (Exception e) { MessageBox.Show(e.ToString()); } } /// <summary> /// 接收数据 /// </summary> /// <param name="obj"></param> private void ReceiveMessage() { IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 0); while (true) { try { if (udpcSend != null) { byte[] bytRecv = udpcSend.Receive(ref remoteIpep); string message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length); ShowMsg(string.Format("{0}[{1}]", remoteIpep, message)); } } catch (Exception ex) { ShowMsg(ex.Message); // break; } } } private void ShowMsg(string msg) { if (txtMsg.InvokeRequired) { this.txtMsg.Invoke(new Action(() => { this.txtMsg.AppendText(msg); })); } else { this.txtMsg.AppendText(msg); } } } }

 

posted @ 2016-05-01 22:36  礼花弹  阅读(200)  评论(0)    收藏  举报