前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >19-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 连接和断开

19-ESP8266 SDK开发基础入门篇--C# TCP客户端编写 , 连接和断开

作者头像
杨奉武
发布2019-07-16 15:43:59
1.1K0
发布2019-07-16 15:43:59
举报
文章被收录于专栏:知识分享知识分享

https://cloud.tencent.com/developer/article/1459318

渐渐的看过去,,,好多节了...

这节做一个C# TCP客户端

新建项目啥子的就不详细截图写了,自行看前面了解 (我的文章只要是有序号的,必须要看前面,因为我所写的教程即是基础又是综合)

先做个这个页面,先做连接和断开

链接TCP用这个变量

其实连接TCP 几句就完了

 我定义了一个函数是因为,其实连接时阻塞的,,所以咱需要开个任务

C# 的任务是这样用

OK  现在测试

由于我是用的台式机,,没有无线网卡,,,所以不能连接WiFi模块了...

我用本机的调试助手测试

 启动

好现在咱用按钮控制连接和断开

代码语言:javascript
复制
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  ,,下拉的时候也刷新下显示

代码语言:javascript
复制
/// <获取本机 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;//显示第一个
                }
            }
        }

然后是下拉事件

代码语言:javascript
复制
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://cloud.tencent.com/developer/article/1464506

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档