http://www.javashuo.com/article/p-gbbgtcpm-e.htmlhtml
渐渐的看过去,,,好多节了...服务器
这节作一个C# TCP客户端函数
新建项目啥子的就不详细截图写了,自行看前面了解 (个人文章只要是有序号的,必需要看前面,由于我所写的教程便是基础又是综合)测试
先作个这个页面,先作链接和断开spa
连接TCP用这个变量线程
其实链接TCP 几句就完了3d
我定义了一个函数是由于,其实链接时阻塞的,,因此咱须要开个任务调试
C# 的任务是这样用code
OK 如今测试orm
因为我是用的台式机,,没有无线网卡,,,因此不能链接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();//刚才写的那个函数 } } }
测试
好,,完全作作成动态的
测试