WinForm之TCP客户端通讯

目录

一 设计界面

二 后台代码


一 设计界面

二 后台代码

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

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

        TcpClient tcpClient = new TcpClient();

        private void connect_Click(object sender, EventArgs e)
        {
            if (!tcpClient.Connected)
            {
                tcpClient.Connect(IP.Text, int.Parse(PORT.Text));

                //开启线程一直读取数据
                Task.Run(() =>
                {
                    while (true)
                    {
                        NetworkStream networkStream = tcpClient.GetStream();
                        if (networkStream != null)
                        {
                            byte[] datas = new byte[1024];
                            networkStream.Read(datas, 0, datas.Length);
                            this.BeginInvoke(new Action(() =>
                            {
                                log.Text = Encoding.UTF8.GetString(datas);
                            }));
                        }
                    }
                });
            }
        }

        private void send_Click(object sender, EventArgs e)
        {
            NetworkStream networkStream = tcpClient.GetStream();
            if (networkStream != null)
            {
                byte[] datas = Encoding.UTF8.GetBytes(log.Text);
                networkStream.Write(datas, 0, datas.Length);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IP.Text = "127.0.0.1";
            PORT.Text = "8899";
        }
    }
}

设计器自动生成代码:

namespace TCP网络客户端通讯
{
    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()
        {
            label1 = new Label();
            label2 = new Label();
            IP = new TextBox();
            PORT = new TextBox();
            connect = new Button();
            log = new RichTextBox();
            send = new Button();
            SuspendLayout();
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Location = new Point(32, 52);
            label1.Name = "label1";
            label1.Size = new Size(22, 20);
            label1.TabIndex = 0;
            label1.Text = "IP";
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.Location = new Point(32, 113);
            label2.Name = "label2";
            label2.Size = new Size(39, 20);
            label2.TabIndex = 1;
            label2.Text = "端口";
            // 
            // IP
            // 
            IP.Location = new Point(103, 52);
            IP.Name = "IP";
            IP.Size = new Size(125, 27);
            IP.TabIndex = 2;
            // 
            // PORT
            // 
            PORT.Location = new Point(103, 110);
            PORT.Name = "PORT";
            PORT.Size = new Size(125, 27);
            PORT.TabIndex = 3;
            // 
            // connect
            // 
            connect.Location = new Point(32, 185);
            connect.Name = "connect";
            connect.Size = new Size(196, 29);
            connect.TabIndex = 4;
            connect.Text = "连接";
            connect.UseVisualStyleBackColor = true;
            connect.Click += connect_Click;
            // 
            // log
            // 
            log.Location = new Point(32, 258);
            log.Name = "log";
            log.Size = new Size(196, 134);
            log.TabIndex = 5;
            log.Text = "";
            // 
            // send
            // 
            send.Location = new Point(32, 416);
            send.Name = "send";
            send.Size = new Size(196, 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(391, 497);
            Controls.Add(send);
            Controls.Add(log);
            Controls.Add(connect);
            Controls.Add(PORT);
            Controls.Add(IP);
            Controls.Add(label2);
            Controls.Add(label1);
            MaximizeBox = false;
            Name = "Form1";
            Text = "TCP客户端通讯";
            Load += Form1_Load;
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private Label label1;
        private Label label2;
        private TextBox IP;
        private TextBox PORT;
        private Button connect;
        private RichTextBox log;
        private Button send;
    }
}

相关推荐

  1. QT TCP通讯客户与服务

    2024-06-16 01:50:01       28 阅读
  2. Qt tcp通信客户+服务器一对一)

    2024-06-16 01:50:01       14 阅读
  3. LwipTCP客户示例记录

    2024-06-16 01:50:01       21 阅读
  4. esp32服务器与android客户tcp通讯

    2024-06-16 01:50:01       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-16 01:50:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-16 01:50:01       18 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-16 01:50:01       17 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-16 01:50:01       20 阅读

热门阅读

  1. 增量数据库同步软件PanguSync侵入式全面清理脚本

    2024-06-16 01:50:01       9 阅读
  2. LeetCode 596, 13, 2

    2024-06-16 01:50:01       7 阅读
  3. 【无标题】

    2024-06-16 01:50:01       5 阅读
  4. React Native 快速Demo(1)

    2024-06-16 01:50:01       6 阅读
  5. 【React】在 react 应用中,怎么使用useReducer

    2024-06-16 01:50:01       9 阅读
  6. 前端面试题日常练-day67 【面试题】

    2024-06-16 01:50:01       6 阅读
  7. IEEE会议论文LaTeX模板中添加页码

    2024-06-16 01:50:01       6 阅读
  8. C++之结构体初始化使用总结

    2024-06-16 01:50:01       7 阅读