WinForm之TCP服务端

作者 : admin 本文共2942个字,预计阅读时间需要8分钟 发布时间: 2024-06-12 共1人阅读

目录

一 原型

二 源码


一 原型

WinForm之TCP服务端插图

WinForm之TCP服务端插图(1)

二 源码

using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TCP网络服务端通讯
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        TcpListener listener = null;
        TcpClient handler = null;
        NetworkStream stream = null;
        bool isrun = false;

        private void openServer_Click(object sender, EventArgs e)
        {

            if (IP.Text.Trim().Length 
                {
                    while (true)
                    {
                        if (isrun && listener != null)
                        {
                            handler = listener.AcceptTcpClient();
                            stream = handler.GetStream();
                            byte[] buffer = new byte[1024];

                            if (stream != null)
                            {
                                stream.Read(buffer, 0, buffer.Length);
                                this.BeginInvoke(new Action(() =>
                                {
                                    log.Text = Encoding.UTF8.GetString(buffer);
                                }));
                            }
                        }
                        Thread.Sleep(50);
                    }
                });
            }
            catch (Exception)
            {

            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isrun = false;
            if (listener != null)
            {
                listener.Stop();
            }

        }
    }
}

设计器自动生成代码:

namespace TCP网络服务端通讯
{
    partial class Form1
    {
        /// 
        ///  Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

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

        #region Windows Form Designer generated code

        /// 
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            label1 = new Label();
            label2 = new Label();
            IP = new TextBox();
            Port = new TextBox();
            openServer = new Button();
            log = new RichTextBox();
            send = new Button();
            SuspendLayout();
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Location = new Point(42, 52);
            label1.Name = "label1";
            label1.Size = new Size(22, 20);
            label1.TabIndex = 0;
            label1.Text = "IP";
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.Location = new Point(42, 102);
            label2.Name = "label2";
            label2.Size = new Size(39, 20);
            label2.TabIndex = 1;
            label2.Text = "端口";
            // 
            // IP
            // 
            IP.Location = new Point(111, 52);
            IP.Name = "IP";
            IP.Size = new Size(291, 27);
            IP.TabIndex = 2;
            // 
            // Port
            // 
            Port.Location = new Point(111, 102);
            Port.Name = "Port";
            Port.Size = new Size(291, 27);
            Port.TabIndex = 3;
            // 
            // openServer
            // 
            openServer.Location = new Point(42, 173);
            openServer.Name = "openServer";
            openServer.Size = new Size(360, 29);
            openServer.TabIndex = 4;
            openServer.Text = "打开服务器";
            openServer.UseVisualStyleBackColor = true;
            openServer.Click += openServer_Click;
            // 
            // log
            // 
            log.Location = new Point(42, 224);
            log.Name = "log";
            log.Size = new Size(360, 238);
            log.TabIndex = 5;
            log.Text = "";
            // 
            // send
            // 
            send.Location = new Point(42, 468);
            send.Name = "send";
            send.Size = new Size(360, 29);
            send.TabIndex = 6;
            send.Text = "发送";
            send.UseVisualStyleBackColor = true;
            send.Click += send_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(9F, 20F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(414, 516);
            Controls.Add(send);
            Controls.Add(log);
            Controls.Add(openServer);
            Controls.Add(Port);
            Controls.Add(IP);
            Controls.Add(label2);
            Controls.Add(label1);
            MaximizeBox = false;
            Name = "Form1";
            Text = "TCP服务端";
            FormClosing += Form1_FormClosing;
            Load += Form1_Load;
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private Label label1;
        private Label label2;
        private TextBox IP;
        private TextBox Port;
        private Button openServer;
        private RichTextBox log;
        private Button send;
    }
}

本站无任何商业行为
个人在线分享 » WinForm之TCP服务端
E-->