UDP聊天示例

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, 13);
            this.ClientSize = new System.Drawing.Size(337, 273);
            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 = "程序处于数据接收状态!";
        }
    }
}
Contributors: FHL