当前位置: 首页 > news >正文

WinForm之TCP服务端

目录

一 原型

二 源码


一 原型

二 源码

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 < 9 && Port.Text.Trim().Length == 0){MessageBox.Show("IP和端口无效");return;}listener = new TcpListener(IPAddress.Parse(IP.Text), int.Parse(Port.Text));listener.Start();isrun = true;}private void send_Click(object sender, EventArgs e){if (stream != null){byte[] buffer = Encoding.UTF8.GetBytes(log.Text);stream.Write(buffer, 0, buffer.Length);}}private void Form1_Load(object sender, EventArgs e){IP.Text = "127.0.0.1";Port.Text = "9800";try{Task.Run(() =>{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{/// <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();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();}#endregionprivate Label label1;private Label label2;private TextBox IP;private TextBox Port;private Button openServer;private RichTextBox log;private Button send;}
}

http://www.lryc.cn/news/372363.html

相关文章:

  • 【TB作品】MSP430 G2553 单片机 口袋板 日历 时钟 闹钟 万年历 电子时钟 秒表显示
  • 推流工具OBS的下载使用
  • 【设计模式之外观模式 -- C++】
  • 【课程总结】Day8(上):深度学习基本流程
  • 论文发表知网//新课程//简介//投稿指南
  • 全面解析AdaBoost:多分类、逻辑回归与混合分类器的实现
  • UE5实战篇二(对话系统1):导语
  • 无人机的发展
  • MySQL和MariaDB的对比和选型
  • Android11 后台启动Activity
  • 这4款国产软件,因为太良心好用,甚至被误认为是外国人开发的
  • 【C++进阶学习】第一弹——继承(上)——探索代码复用的乐趣
  • OpenCV单词轮廓检测
  • 主流后端开发语言对比
  • Linux排查问题常用命令
  • 【Python/Pytorch - 网络模型】-- 手把手搭建E3D LSTM网络
  • C#面:Server.UrlEncode、HttpUtility.UrlDecode的区别
  • 50.Python-web框架-Django中引入静态的bootstrap样式
  • 机器学习实验----支持向量机(SVM)实现二分类
  • STM32自己从零开始实操05:接口电路原理图
  • git子模块
  • stm32编写Modbus步骤
  • 基于 Transformer 的大语言模型
  • 证照之星是一款很受欢迎的证件照制作软件
  • 不定时更新 解决无法访问GitHub github.com 打不开 dns访问加速
  • 单向环形链表的创建与判断链表是否有环
  • JVM堆栈的区别、分配内存与并发安全问题、对象定位
  • Python教程:机器学习 - 百分位数(4)
  • 数据结构习题(快期末了)
  • Http协议:Http缓存