发布时间:2024-02-16 14:00
1、点击工程项目,然后右键,选择“管理NuGet程序包”,如下:
2、出现如下界面,然后在搜索框输入BytesIO,然后安装下面的BytesIO.TCP的程序包。
注意BytesIO.TCP是依赖.NET框架4.5以上的版本的,所以创建工程的时候要选择高点的版本。
程序界面如下,做得比较丑。
其中Host IP是远程主机IP,Host Port是远程主机端口号。
在Form()函数中,主要做了生成窗体时,获取了本机所有网卡的IP地址,然后预设了一些控件的默认值。最后还监听了一些tcp client相关的事件,代码如下:
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
// 获取本地可用的IP地址
string strHostIP = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(strHostIP);
foreach (IPAddress ipaddr in ipadrlist)
{
if (ipaddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
cbbHostIP.Items.Add(ipaddr.ToString());
}
cbbHostIP.Items.Add("127.0.0.1"); // 最后加上回环测试地址
cbbHostIP.SelectedIndex = 0;
tbHostPort.Text = "8080";
client.OnDataReceived += Client_OnDataReceived; // 接收事件函数
client.OnConnectedSuccessfully += Client_OnConnectedSuccessfully; // 连接成功事件函数
client.OnDisconnected += Client_OnDisconnected; // 断开连接事件函数
}
在界面上输入远程主机IP和对应的远程主机端口后,点击一下按钮,就会去连接这个服务端了,点击事件函数如下:
private void btnConnect_Click(object sender, EventArgs e)
{
if (client.IsConnected == false)
{
client.Host = cbbHostIP.Text; // 远程主机IP
client.Port = int.Parse(tbHostPort.Text); // 远程主机端口
client.Connect();
}
else
{
client.Disconnect();
}
}
IsConnected属性是BytesIO.TCP程序包自带的一个属性,我们可以通过这个属性判断当前是否已经连接了,如果没有连接那么点击一下,就调用 client.Connect(); 函数进行连接即可。
其他各部分源码看下面的整体工程代码…
using System;
using System.Collections.Generic;
using STTech.BytesIO.Core;
using STTech.BytesIO.Tcp;
using System.Windows.Forms;
using System.Net;
namespace BytesIO_TCP_Client
{
public partial class Form1 : Form
{
private TcpClient client = new TcpClient();
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
// 获取本地可用的IP地址
string strHostIP = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(strHostIP);
foreach (IPAddress ipaddr in ipadrlist)
{
if (ipaddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
cbbHostIP.Items.Add(ipaddr.ToString());
}
cbbHostIP.Items.Add("127.0.0.1"); // 最后加上回环测试地址
cbbHostIP.SelectedIndex = 0;
tbHostPort.Text = "8080";
client.OnDataReceived += Client_OnDataReceived; // 接收事件函数
client.OnConnectedSuccessfully += Client_OnConnectedSuccessfully; // 连接成功事件函数
client.OnDisconnected += Client_OnDisconnected; // 断开连接事件函数
}
private void Client_OnDisconnected(object sender, STTech.BytesIO.Core.Entity.DisconnectedEventArgs e)
{
Print($"客户端已断开({e.ReasonCode})");
btnConnect.Text = "请求连接";
}
private void Client_OnConnectedSuccessfully(object sender, STTech.BytesIO.Core.Entity.ConnectedSuccessfullyEventArgs e)
{
Print("The server is connected from local " + client.Host + ":" + client.LocalPort);
btnConnect.Text = "断开连接";
}
private void Client_OnDataReceived(object sender, STTech.BytesIO.Core.Entity.DataReceivedEventArgs e)
{
Print($"Receive Data:{e.Data.EncodeToString("GBK")}");
}
private void Print(string msg)
{
tbRecv.AppendText($"[{DateTime.Now.ToString("yyy-MM-dd HH.mm.ss.fff")}] {msg}\r\n");
}
private void btnSend_Click(object sender, EventArgs e)
{
client.Send(tbSend.Text.GetBytes("GBK"));
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (client.IsConnected == false)
{
client.Host = cbbHostIP.Text;
client.Port = int.Parse(tbHostPort.Text);
client.Connect();
}
else
{
client.Disconnect();
}
}
}
}
整个工程的源码上传了CSDN,下载链接如下:
https://download.csdn.net/download/luobeihai/86239773