https://cloud.tencent.com/developer/article/1459318
渐渐的看过去,,,好多节了...
这节做一个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();//刚才写的那个函数
}
}
}
测试
好,,彻底做做成动态的
测试
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有