Socket編程 消息傳送 UDP協議(窗口實現) 服務器

//code

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace UDP_xzh_server
{
    public partial class Form1 : Form
    {
        Socket m_UdpServer = null;
        Thread th = null;
        int i = 0;
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        //start click
        private void button1_Click(object sender, EventArgs e)
        {
            Start();
        }

        //stop click
        private void button2_Click(object sender, EventArgs e)
        {
            Stop();
        }

        //getMessage
        void getMsg()
        {   
            while (true)
            {
                try
                {
                    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 9999);
                    EndPoint Remote = (EndPoint)(sender); 
                    byte[] data = new byte[1024];
                    int recv = m_UdpServer.ReceiveFrom(data, ref Remote);
                    textBox1.AppendText(ASCIIEncoding.ASCII.GetString(data));
                    m_UdpServer.SendTo(data, Remote);
                    Console.WriteLine(++i + "     value of i");    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    throw ex;
                }
            }
        }


        //stop
        public void Stop()
        {
            if (true)
            {
                try
                {
                    m_UdpServer.Shutdown(SocketShutdown.Both);
                    m_UdpServer.Close();
                    th.Abort();
                    Console.WriteLine("close!!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

 

        //start
        public void Start()
        {
            if (true)
            {
                try
                {
                    m_UdpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    IPAddress ip = IPAddress.Parse("127.0.0.1");
                    IPEndPoint ipep = new IPEndPoint(ip, 9999);
                    m_UdpServer.Bind(ipep);
                    Console.WriteLine("start!!");
                    textBox1.AppendText("server start!");
                    Thread thread = new Thread(getMsg);
                    thread.IsBackground = true;
                    thread.Start();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

        }
    }
}

 

 

//design code

 

namespace UDP_xzh_server
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(33, 227);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "start";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(161, 227);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 0;
            this.button2.Text = "stop";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(12, 24);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(260, 197);
            this.textBox1.TabIndex = 1;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.TextBox textBox1;
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章