Socket编程 消息传送 UDP协议(窗口实现) 客户端

来源:互联网 发布:网络信息发布审查制度 编辑:程序博客网 时间:2024/05/18 06:57

//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.Net;
using System.Threading;
namespace UDP_XZH_Client
{
    public partial class Form1 : Form
    {
        Socket Client = null;
        int port = 9999;
        IPAddress ip = IPAddress.Parse("127.0.0.1");
        IPEndPoint serverIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
        int i = 0;

        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            button2.Enabled = true;
            button3.Enabled = false;
        }

 

        //start
        private void button2_Click(object sender, EventArgs e)
        {
            button3.Enabled = true;
            button2.Enabled = false;
            start();
        }


        //stop
        private void button3_Click(object sender, EventArgs e)
        {
            button2.Enabled = true;
            button3.Enabled = false;
            stop();
        }

        //send
        private void button1_Click(object sender, EventArgs e)
        {
            send();
        }

 

        //init
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //start
        private void start()
        {
            try
            {
                Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                byte[] data = new byte[1024];
                Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
                IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
                string welcome = "hello! ";
                data = Encoding.ASCII.GetBytes(welcome);
                Client.SendTo(data, data.Length, SocketFlags.None, ip);
                textBox1.AppendText("start success!!");
                Thread thread = new Thread(getMsg);
                thread.IsBackground = true;
                thread.Start();
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.ToString());
            }
        }

        //stop
        private void stop()
        {
            try
            {
                Client.Close();
                textBox1.AppendText("client stop!");
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.ToString());
            }
        }


        //send
        private void send()
        {
            try
            {
                IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
                string input = textBox2.Text;
                Client.SendTo(Encoding.ASCII.GetBytes(input), Encoding.ASCII.GetBytes(input).Length, SocketFlags.None, ip);
                byte[] data = new byte[1024];
                Console.WriteLine(input);
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.ToString());
            }
        }


        //getMsg
        private void getMsg()
        {
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            //string input;
            EndPoint Remote = (EndPoint)sender;
            try
            {
                while (true)
                {
                    Byte[] data = new byte[1024];
                    int recv = Client.ReceiveFrom(data, ref Remote);
                    textBox1.AppendText(ASCIIEncoding.ASCII.GetString(data));
                    Console.WriteLine(++i+"     value of i");
                }

            }
            catch (SocketException se)
            {
                Console.WriteLine(se.ToString());
            }
        }
    }
}

 

//design code

 

namespace UDP_XZH_Client
{
    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.button3 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(197, 207);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(24, 13);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(145, 12);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(24, 51);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox1.Size = new System.Drawing.Size(223, 136);
            this.textBox1.TabIndex = 3;
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(24, 209);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(167, 21);
            this.textBox2.TabIndex = 3;
            //
            // 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.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

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

 

 

原创粉丝点击