zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    golang/windows如何删除只读属性文件
    golang/TLS 采坑
    gsweb —— 理解HTTP协议
    gsweb —— 自己动手用golang写WEB框架
    Scala冒泡排序、快排、归并
    Hadoop自动化部署脚本
    大数据学习笔记
    vim键盘图
    什么是回调或高级函数?
    使用CSS表达式去除超链接的虚框的一些方法
  • 原文地址:https://www.cnblogs.com/yangfengwu/p/11192594.html
Copyright © 2011-2022 走看看