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

// 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 Socket_xzh
{
    public partial class Form1 : Form
    {
        bool state = false;
       
        Socket server= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPAddress ip = IPAddress.Parse("127.0.0.1");
        private int port = 9999;
        Byte[] result=new Byte[1024];

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

        //start
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled=false;
            button2.Enabled = true;
            state = true;
            serverState(state);
        }


        //stop
        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
            state = false;
            serverState(state);
        }

        //server
        private void serverState(bool state)
        {
            if(state)
            {
                StartServer();
            }
            else
            {
                CloseServer();
            }
        }

        //開啓服務
        private void StartServer()
        {
            server.Bind(new IPEndPoint(ip, port));
            server.Listen(100);
            Thread thread = new Thread(ListenAction);
            thread.Start();
            textBox1.AppendText("server start!!");
        }


        //關閉服務
        private void CloseServer()
        {
            server.Close();
            textBox1.AppendText("server stop!!");
        }

        //監聽函數
        private void ListenAction()
        {
            Socket listen = server.Accept();
            listen.Send(ASCIIEncoding.ASCII.GetBytes("hello,linked to" + listen.LocalEndPoint.ToString()));
            Thread thread = new Thread(GetMessage);
            thread.Start(listen);
        }


        private void GetMessage(object socket)
        {
            result = new Byte[1024];
            Socket sendSocket=(Socket) socket;
            while (true)
            {
                int msgNum = sendSocket.Receive(result);
                TextBox.CheckForIllegalCrossThreadCalls = false;
                textBox1.AppendText("\r\nclient msg:" + ASCIIEncoding.ASCII.GetString(result));
                sendSocket.Send(ASCIIEncoding.ASCII.GetBytes("reserver msg:" + ASCIIEncoding.ASCII.GetString(result)));
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

 

//design code

 

namespace Socket_xzh
{
    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.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(29, 227);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "開始";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(146, 227);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "停止";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(27, 32);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(245, 24);
            this.label1.TabIndex = 2;
            this.label1.Text = "本程序模擬服務器接收來自硬件終端的數據,\r\n       並將讀取到的數據寫入數據庫中";
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(29, 68);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox1.Size = new System.Drawing.Size(216, 136);
            this.textBox1.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.textBox1);
            this.Controls.Add(this.label1);
            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.Label label1;
        private System.Windows.Forms.TextBox textBox1;
    }
}

 

 

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