🔌 TCP 传输
命名空间: Snet.Core.communication.net.tcp | 类: TcpClientOperate, TcpServiceOperate
TcpClientOperate — TCP 客户端
继承: CommunicationAbstract<TcpClientOperate, TcpClientData.Basics> | 接口: ICommunication
配置 (TcpClientData.Basics)
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
IpAddress |
string |
"127.0.0.1" |
远程服务器 IP |
Port |
int |
6688 |
远程服务器端口 |
InterruptReconnection |
bool |
true |
断线自动重连 |
ReconnectionInterval |
int |
1000 |
重连间隔 (ms) |
Timeout |
int |
1000 |
I/O 超时 (ms) |
SendWaitInterval |
int |
5000 |
发送等待超时 (ms) |
MaxChunkSize |
int |
261120 |
每块最大字节数 (255KB) |
RetrySendCount |
int |
5 |
每块重试次数 |
BufferSize |
int |
1048576 |
接收缓冲区 (1MB) |
API
// 连接 — 创建 TcpClient,连接远程端点,启动 MonitorAsync 读循环
await client.OnAsync();
// 断开 — 取消监控 + 重连 + 关闭 TcpClient
await client.OffAsync(bool hardClose = false);
// 发送 — 通过 NetworkStream 发送,自动分包按块重试
await client.SendAsync(byte[] data);
// 发送并等待响应 — 停止监控 → 发送 → 读取响应 → 恢复监控
await client.SendWaitAsync(byte[] data, CancellationToken token);
// 底层对象 — 返回 TcpClient 实例
await client.GetBaseObjectAsync();
// 状态 — 检查 _communication != null && _communication.Connected
await client.GetStatusAsync();
断线重连
指数退避: 初始 500ms → 最大 30s。通过 Socket.Poll 检测连接状态。
使用示例
using Snet.Core.communication.net.tcp.client;
using static Snet.Core.communication.net.tcp.client.TcpClientData;
var config = new Basics { IpAddress = "192.168.1.100", Port = 6688 };
var client = await TcpClientOperate.InstanceAsync(config);
client.OnDataEvent += (s, e) => {
if (e.Status && e.ResultData is byte[] data)
Console.WriteLine($"收到: {data.Length} 字节");
};
await client.OnAsync();
await client.SendAsync(Encoding.UTF8.GetBytes("PING"));
var resp = await client.SendWaitAsync(Encoding.UTF8.GetBytes("STATUS"), new CancellationTokenSource(5000).Token);
await client.OffAsync();
TcpServiceOperate — TCP 服务端
继承: CommunicationServiceAbstract<TcpServiceOperate, TcpServiceData.Basics> | 接口: ICommunicationService
配置 (TcpServiceData.Basics)
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
IpAddress |
string? |
"127.0.0.1" |
监听地址 |
Port |
int |
6688 |
监听端口 |
MaxNumber |
int |
1000 |
最大连接数 |
MaxChunkSize |
int |
261120 |
每块最大字节数 |
RetrySendCount |
int |
5 |
每块重试次数 |
Timeout |
int |
1000 |
I/O 超时 (ms) |
BufferSize |
int |
1048576 |
接收缓冲区 (1MB) |
客户端事件
OnDataEvent 中的 ClientMessage:
| 字段 | 类型 | 说明 |
|---|---|---|
Step |
Steps 枚举 |
客户端连接(0) / 客户端断开(1) / 消息接收(2) |
IpPort |
string |
客户端 IP:端口 |
Bytes |
byte[]? |
接收数据 |
使用示例
using Snet.Core.communication.net.tcp.service;
using static Snet.Core.communication.net.tcp.service.TcpServiceData;
var config = new Basics { IpAddress = "0.0.0.0", Port = 6688 };
var server = await TcpServiceOperate.InstanceAsync(config);
server.OnDataEvent += (s, e) => {
if (e.ResultData is ClientMessage msg) {
Console.WriteLine($"[{msg.Step}] {msg.IpPort}: {msg.Bytes?.Length} 字节");
}
};
await server.OnAsync();
await server.SendAsync(Encoding.UTF8.GetBytes("广播")); // 群发
await server.SendAsync(Encoding.UTF8.GetBytes("你好"), "192.168.1.10:12345"); // 单播
await server.RemoveAsync(new[] { "192.168.1.10:12345" }); // 踢出
await server.OffAsync();
