BytesIO | 零基础轻松看懂 C# TCP客户端(完整源码+视频教程)
作者:互联网
BytesIO系列(一) 轻松实现TCP客户端
@
视频教程
B站传送门
https://www.bilibili.com/video/BV1jB4y1R7fx/
动手实现
引用库
首先,通过NuGetj将STTech.BytesIO.Tcp
引用到项目中;
界面设计
客户端的界面设计如下图:
源代码
using STTech.BytesIO.Core;
using STTech.BytesIO.Tcp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Demo.BytesIO.Client
{
public partial class Form1 : Form
{
private BytesClient client;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
client = new TcpClient() ;
propertyGrid.SelectedObject = client;
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})");
}
private void Client_OnConnectedSuccessfully(object sender, STTech.BytesIO.Core.Entity.ConnectedSuccessfullyEventArgs e)
{
Print("连接成功");
}
private void Client_OnDataReceived(object sender, STTech.BytesIO.Core.Entity.DataReceivedEventArgs e)
{
Print($"收到数据:{e.Data.EncodeToString("GBK")}");
}
private void btnConnect_Click(object sender, EventArgs e)
{
client.Connect();
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
client.Disconnect();
}
private void btnSend_Click(object sender, EventArgs e)
{
client.Send(tbSend.Text.GetBytes("GBK"));
}
private void Print(string msg)
{
tbRecv.AppendText($"[{DateTime.Now}] {msg}\r\n");
}
}
}
结束语
如果非IT行业的女朋友都能学会的话,应该就算0基础入门的教学视频了吧!
超简单的C# TCP开发入门,短短的代码,完整的功能,掏出你的VS码一个试试手吧!
关于BytesIO还有很多的开发技巧可以分享 ,在其余教程未完成之前,如果有使用上的疑问可以加群讨论。
QQ群:738018341
标签:sender,C#,void,private,client,源码,TCP,Client,BytesIO 来源: https://www.cnblogs.com/landriesnidis/p/16319729.html