19-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 连接和断开
https://www.cnblogs.com/yangfengwu/p/11130428.html
渐渐的看过去,,,好多节了...
这节做一个C# TCP客户端
新建项目啥子的就不详细截图写了,自行看前面了解 (我的文章只要是有序号的,必须要看前面,因为我所写的教程即是基础又是综合)
先做个这个页面,先做连接和断开
链接TCP用这个变量
其实连接TCP 几句就完了
我定义了一个函数是因为,其实连接时阻塞的,,所以咱需要开个任务
C# 的任务是这样用
OK 现在测试
由于我是用的台式机,,没有无线网卡,,,所以不能连接WiFi模块了...
我用本机的调试助手测试
启动
好现在咱用按钮控制连接和断开
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; namespace TCPClient { public partial class Form1 : Form { private TcpClient myTcpClient = null;// TcpClient Thread ConnectThread;//连接线程 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void ConnectMethod() { myTcpClient = new TcpClient(); //实例化myTcpClient try { myTcpClient.Connect("192.168.1.2", 60000);//连接服务器 //连接上以后往下执行 Invoke((new Action(() => { button1.Text = "断开"; }))); } catch (Exception) { //异常处理函数 Invoke((new Action(() => { button1.Text = "连接"; }))); try { myTcpClient.Close(); }catch { } //关闭连接 } } //连接和断开按钮 private void button1_Click(object sender, EventArgs e) { if (button1.Text == "连接") { ConnectThread = new Thread(ConnectMethod);//创建任务 ConnectThread.Start();//启动任务 } else { try { myTcpClient.Close(); } catch { } //关闭连接 Invoke((new Action(() => { button1.Text = "连接"; }))); } } } }
测试
接着用上
首先做个功能,,一开始IP 那个下拉框,显示出来电脑的IP ,,下拉的时候也刷新下显示
/// <获取本机 IP 地址> /// /// </summary> /// <returns></returns> private void getIPAddress() { IPAddress[] hostipspool = Dns.GetHostAddresses("");//获取本机所以IP comboBox1.Items.Clear();//清除下拉框里面的内容 foreach (IPAddress ipa in hostipspool) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { comboBox1.Items.Add(ipa.ToString());//下拉框加入IP数据 comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//显示第一个 } } }
然后是下拉事件
namespace TCPClient { public partial class Form1 : Form { private TcpClient myTcpClient = null;// TcpClient Thread ConnectThread;//连接线程 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { getIPAddress();//刚才写的那个函数.获取电脑IP,并显示在下拉框 } /// <获取本机 IP 地址> /// /// </summary> /// <returns></returns> private void getIPAddress() { IPAddress[] hostipspool = Dns.GetHostAddresses("");//获取本机所以IP comboBox1.Items.Clear();//清除下拉框里面的内容 foreach (IPAddress ipa in hostipspool) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { comboBox1.Items.Add(ipa.ToString());//下拉框加入IP数据 comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//显示第一个 } } } private void ConnectMethod() { myTcpClient = new TcpClient(); //实例化myTcpClient try { myTcpClient.Connect("192.168.1.2", 60000);//连接服务器 //连接上以后往下执行 Invoke((new Action(() => { button1.Text = "断开"; }))); } catch (Exception) { //异常处理函数 Invoke((new Action(() => { button1.Text = "连接"; }))); try { myTcpClient.Close(); }catch { } //关闭连接 } } //连接和断开按钮 private void button1_Click(object sender, EventArgs e) { if (button1.Text == "连接") { ConnectThread = new Thread(ConnectMethod);//创建任务 ConnectThread.Start();//启动任务 } else { try { myTcpClient.Close(); } catch { } //关闭连接 Invoke((new Action(() => { button1.Text = "连接"; }))); } } private void comboBox1_DropDown(object sender, EventArgs e) { getIPAddress();//刚才写的那个函数 } } }
测试
好,,彻底做做成动态的
测试
https://www.cnblogs.com/yangfengwu/p/11192603.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-07-16 STM32嵌入LUA开发(控制小灯闪耀)