TCPSimple 1.0.5
dotnet add package TCPSimple --version 1.0.5
NuGet\Install-Package TCPSimple -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TCPSimple" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TCPSimple" Version="1.0.5" />
<PackageReference Include="TCPSimple" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TCPSimple --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TCPSimple, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TCPSimple@1.0.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TCPSimple&version=1.0.5
#tool nuget:?package=TCPSimple&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
TCPSimple
基于长度前缀法的 TCP 通信库,支持服务端与客户端双向通信,兼容 .NET 5.0+
🌟 核心特性
- ✅ 自动处理 TCP 粘包/半包问题(基于长度前缀法)
- ✅ 支持多客户端连接管理与并发通信
- ✅ 异步非阻塞设计,性能高效
- ✅ 完善的异常处理与事件通知
- ✅ 兼容 .NET 5.0 及以上所有版本
🚀 快速上手
服务端示例
using TCPSimple.Server;
using TCPSimple.Common;
using System.Net;
// 1. 配置服务端
var serverOptions = new TcpServerOptions
{
IpAddress = IPAddress.Any, // 监听所有网卡
Port = 8888, // 监听端口
MaxConnections = 50, // 最大连接数
ReceiveTimeout = 30000 // 接收超时(毫秒)
};
// 2. 实例化服务端并注册消息处理逻辑
var server = new TcpServer(serverOptions, async (server, clientId, message) =>
await HandleMessage(server, clientId, message)
);
// 3. 订阅事件(可选)
server.ClientDisconnected += clientId =>
Console.WriteLine($"客户端 [{clientId}] 已断开连接");
server.ErrorOccurred += ex =>
Console.WriteLine($"发生异常: {ex.Message}");
// 4. 启动服务
server.Start();
Console.WriteLine("服务端已启动,按任意键停止...");
Console.ReadKey();
// 5. 停止服务(释放资源)
server.Stop();
// 消息处理方法
async Task HandleMessage(TcpServer server, string clientId, string message)
{
Console.WriteLine($"收到客户端 [{clientId}] 的消息: {message}");
// 回复客户端
await server.SendToClientAsync(clientId, $"服务端已收到: {message}");
// 广播消息给所有客户端(可选)
await server.BroadcastAsync($"通知:客户端 [{clientId}] 发送了消息");
}
客户端示例
using TCPSimple.Client;
using TCPSimple.Exceptions;
using Newtonsoft.Json;
// 1. 配置客户端
var clientOptions = new TcpClientOptions
{
ServerIp = "127.0.0.1", // 服务端 IP
ServerPort = 8888, // 服务端端口
ConnectTimeout = 5000, // 连接超时(毫秒)
ReceiveTimeout = 30000 // 接收超时(毫秒)
};
// 2. 实例化客户端并注册消息接收回调
var client = new TcpClient(clientOptions,(string message) =>
Console.WriteLine($"收到服务端消息: {message}")
);
// 3. 订阅事件(可选)
client.Disconnected += () =>
Console.WriteLine("与服务端的连接已断开");
client.ErrorOccurred += ex =>
Console.WriteLine($"发生异常: {JsonConvert.SerializeObject(ex)}");
// 4. 连接并交互
try
{
await client.ConnectAsync();
Console.WriteLine("连接成功,输入消息发送(空行退出):");
string? input;
while ((input = Console.ReadLine()) != null)
{
if (string.IsNullOrEmpty(input)) break;
await client.SendAsync(input);
}
}
catch (TcpConnectionException ex)
{
Console.WriteLine($"连接失败: {ex.Message}");
}
finally
{
client.Disconnect(); // 主动断开连接
}
📦 安装
通过 NuGet 安装(推荐):
dotnet add package TCPSimple
或直接克隆源码编译:
git clone https://github.com/your-username/TCPSimple.git
cd TCPSimple
dotnet build -c Release
📚 协议说明(长度前缀法)
消息格式为 4字节长度头(网络字节序) + 实际数据(UTF-8 编码):
- 长度头:
int类型,标识后续数据的字节数(需用IPAddress.HostToNetworkOrder转换为网络字节序) - 实际数据:业务消息的 UTF-8 字节流
示例
发送消息 "Hello" 时,实际传输的字节流为:
[0x00, 0x00, 0x00, 0x05] // 长度头(5字节)
[0x48, 0x65, 0x6c, 0x6c, 0x6f] // "Hello" 的 UTF-8 字节
❓ 常见问题
Q:如何处理大文件传输?
A:可调整 TcpConstants.MaxMessageSize(默认 1MB),或分片传输大文件(将文件拆分为多个消息,服务端重组)。
Q:客户端重连逻辑如何实现?
A:可在 Disconnected 事件中添加重连逻辑:
client.Disconnected += async () =>
{
Console.WriteLine("尝试重连...");
await Task.Delay(3000);
try { await client.ConnectAsync(); }
catch { /* 重连失败处理 */ }
};
Q:支持跨平台吗?
A:完全支持,可在 Windows、Linux、macOS 上运行(需安装对应平台的 .NET SDK)。
🛠️ 贡献指南
- Fork 本仓库
- 新建分支
git checkout -b feature/your-feature - 提交代码并发起 Pull Request
- 等待代码审查与合并
📄 许可证
MIT © [HELPLIESS]
Copyright (c) [2025] [HELPLIESS]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net5.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.