https://www.cnblogs.com/yangfengwu/p/11130428.html
Gradually look at the past,,, a lot of knots...
This section is a C# TCP client
I won’t write detailed screenshots of the new project, and read the previous to understand it myself (as long as my article has a serial number, I must look at the front, because the tutorial I wrote is both basic and comprehensive)
Make this page first, connect and disconnect first
Link TCP uses this variable
Actually connect to TCP in just a few sentences
I defined a function because, in fact, it is blocked when connecting, so we need to open a task
The task of C# is to use it like this
OK test now
Since I am using a desktop computer, there is no wireless network card, so I can't connect to the WiFi module...
I use the debugging assistant of this machine to test
start up
Okay now let's use buttons to control connection and disconnection
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(); //Instantiate myTcpClient try { myTcpClient.Connect("192.168.1.2", 60000);//Connect to the server //After connecting, execute Invoke((new Action) (() => { button1.Text = "Disconnect"; }))); } catch (Exception) { //Exception handling function Invoke((new Action(() => { button1.Text = "Connect"; }))); try {myTcpClient.Close(); }catch {} //Close the connection } } //Connect and disconnect button private void button1_Click(object sender, EventArgs e) { if (button1.Text == "Connect") { ConnectThread = new Thread(ConnectMethod);//Create task ConnectThread.Start();//Start task } else { try {myTcpClient.Close();} catch {} //Close the connection Invoke((new Action(()) => { button1.Text = "Connect"; }))); } } } }
test
Then use
First, make a function. At the beginning, the drop-down box of IP displays the IP of the computer. When it is pulled down, the display is refreshed.
/// <Get the local IP address> /// /// </summary> /// <returns></returns> private void getIPAddress() { IPAddress[] hostipspool = Dns.GetHostAddresses("");/ /Get this machine so IP comboBox1.Items.Clear();//Clear the content in the drop-down box foreach (IPAddress ipa in hostipspool) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { comboBox1.Items.Add(ipa. ToString());//Add IP data to the drop-down box comboBox1.SelectedIndex = comboBox1.Items.Count> 0? 0: -1;//Display the first one } } }
Then there is the drop-down event
namespace TCPClient { public partial class Form1: Form { private TcpClient myTcpClient = null;// TcpClient Thread ConnectThread;//Connect thread public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { getIPAddress() ;//The function just written. Get the computer IP and display it in the drop-down box } /// <Get the local IP address> /// /// </summary> /// <returns></returns> private void getIPAddress() { IPAddress[] hostipspool = Dns.GetHostAddresses("");//Get the IP of this machine comboBox1.Items.Clear();//Clear the contents of the drop-down box foreach (IPAddress ipa in hostipspool) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { comboBox1.Items.Add(ipa.ToString());/ /Add the IP data to the drop-down box comboBox1.SelectedIndex = comboBox1.Items.Count> 0? 0: -1;//Display the first one } } } private void ConnectMethod() { myTcpClient = new TcpClient(); //Instantiate myTcpClient try { myTcpClient.Connect("192.168.1.2", 60000);//Connect to the server //After connecting, execute Invoke((new Action() () => { button1.Text = "Disconnect"; }))); } catch (Exception) { //Exception handling function Invoke((new Action(() => { button1.Text = "Connect"; }))); try {myTcpClient.Close(); }catch {} //Close the connection } } //Connect and disconnect button private void button1_Click(object sender, EventArgs e) { if (button1.Text == "Connect") { ConnectThread = new Thread(ConnectMethod);//Create task ConnectThread.Start ();//Start task } else { try {myTcpClient.Close();} catch {} //Close the connection Invoke((new Action(() => { button1.Text = "Connect"; }))); } } private void comboBox1_DropDown(object sender, EventArgs e) { getIPAddress();//The function just written } } }
test
Okay, make it completely dynamic
test