• WinForm之TCP客户端通讯


    目录

    一 设计界面

    二 后台代码


    一 设计界面

    二 后台代码

    1. using System.Net.Sockets;
    2. using System.Text;
    3. namespace TCP网络客户端通讯
    4. {
    5. public partial class Form1 : Form
    6. {
    7. public Form1()
    8. {
    9. InitializeComponent();
    10. }
    11. TcpClient tcpClient = new TcpClient();
    12. private void connect_Click(object sender, EventArgs e)
    13. {
    14. if (!tcpClient.Connected)
    15. {
    16. tcpClient.Connect(IP.Text, int.Parse(PORT.Text));
    17. //开启线程一直读取数据
    18. Task.Run(() =>
    19. {
    20. while (true)
    21. {
    22. NetworkStream networkStream = tcpClient.GetStream();
    23. if (networkStream != null)
    24. {
    25. byte[] datas = new byte[1024];
    26. networkStream.Read(datas, 0, datas.Length);
    27. this.BeginInvoke(new Action(() =>
    28. {
    29. log.Text = Encoding.UTF8.GetString(datas);
    30. }));
    31. }
    32. }
    33. });
    34. }
    35. }
    36. private void send_Click(object sender, EventArgs e)
    37. {
    38. NetworkStream networkStream = tcpClient.GetStream();
    39. if (networkStream != null)
    40. {
    41. byte[] datas = Encoding.UTF8.GetBytes(log.Text);
    42. networkStream.Write(datas, 0, datas.Length);
    43. }
    44. }
    45. private void Form1_Load(object sender, EventArgs e)
    46. {
    47. IP.Text = "127.0.0.1";
    48. PORT.Text = "8899";
    49. }
    50. }
    51. }

    设计器自动生成代码

    1. namespace TCP网络客户端通讯
    2. {
    3. partial class Form1
    4. {
    5. ///
    6. /// Required designer variable.
    7. ///
    8. private System.ComponentModel.IContainer components = null;
    9. ///
    10. /// Clean up any resources being used.
    11. ///
    12. /// true if managed resources should be disposed; otherwise, false.
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. ///
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. ///
    26. private void InitializeComponent()
    27. {
    28. label1 = new Label();
    29. label2 = new Label();
    30. IP = new TextBox();
    31. PORT = new TextBox();
    32. connect = new Button();
    33. log = new RichTextBox();
    34. send = new Button();
    35. SuspendLayout();
    36. //
    37. // label1
    38. //
    39. label1.AutoSize = true;
    40. label1.Location = new Point(32, 52);
    41. label1.Name = "label1";
    42. label1.Size = new Size(22, 20);
    43. label1.TabIndex = 0;
    44. label1.Text = "IP";
    45. //
    46. // label2
    47. //
    48. label2.AutoSize = true;
    49. label2.Location = new Point(32, 113);
    50. label2.Name = "label2";
    51. label2.Size = new Size(39, 20);
    52. label2.TabIndex = 1;
    53. label2.Text = "端口";
    54. //
    55. // IP
    56. //
    57. IP.Location = new Point(103, 52);
    58. IP.Name = "IP";
    59. IP.Size = new Size(125, 27);
    60. IP.TabIndex = 2;
    61. //
    62. // PORT
    63. //
    64. PORT.Location = new Point(103, 110);
    65. PORT.Name = "PORT";
    66. PORT.Size = new Size(125, 27);
    67. PORT.TabIndex = 3;
    68. //
    69. // connect
    70. //
    71. connect.Location = new Point(32, 185);
    72. connect.Name = "connect";
    73. connect.Size = new Size(196, 29);
    74. connect.TabIndex = 4;
    75. connect.Text = "连接";
    76. connect.UseVisualStyleBackColor = true;
    77. connect.Click += connect_Click;
    78. //
    79. // log
    80. //
    81. log.Location = new Point(32, 258);
    82. log.Name = "log";
    83. log.Size = new Size(196, 134);
    84. log.TabIndex = 5;
    85. log.Text = "";
    86. //
    87. // send
    88. //
    89. send.Location = new Point(32, 416);
    90. send.Name = "send";
    91. send.Size = new Size(196, 29);
    92. send.TabIndex = 6;
    93. send.Text = "发送数据";
    94. send.UseVisualStyleBackColor = true;
    95. send.Click += send_Click;
    96. //
    97. // Form1
    98. //
    99. AutoScaleDimensions = new SizeF(9F, 20F);
    100. AutoScaleMode = AutoScaleMode.Font;
    101. ClientSize = new Size(391, 497);
    102. Controls.Add(send);
    103. Controls.Add(log);
    104. Controls.Add(connect);
    105. Controls.Add(PORT);
    106. Controls.Add(IP);
    107. Controls.Add(label2);
    108. Controls.Add(label1);
    109. MaximizeBox = false;
    110. Name = "Form1";
    111. Text = "TCP客户端通讯";
    112. Load += Form1_Load;
    113. ResumeLayout(false);
    114. PerformLayout();
    115. }
    116. #endregion
    117. private Label label1;
    118. private Label label2;
    119. private TextBox IP;
    120. private TextBox PORT;
    121. private Button connect;
    122. private RichTextBox log;
    123. private Button send;
    124. }
    125. }

  • 相关阅读:
    复制粘贴,快速将Python程序打包成exe
    【JavaScript复习十七】作用域以及变量提升
    树莓派等Linux开发板上使用 SSD1306 OLED 屏幕,bullseye系统 ubuntu,debian
    REDIS可视化神器 | REDIS DESK MANAGER(2022.5.1)
    springboot+共享电动单车管理系统 毕业设计-附源码131016
    【vue3】12.跟着官网学习vue3-侦听器,watch方法
    SpringSecurity学习笔记(一)springSecurity的整体架构
    高并发下如何避免产生重复数据?
    15:00面试,15:06就出来了,问的问题有点变态。。。
    CSS的break-inside 属性 的使用
  • 原文地址:https://blog.csdn.net/XiaoWang_csdn/article/details/139610369