NetCoreServer 官方文档 完整中文翻译版
官方链接
1. 项目简介
NetCoreServer 是一款超高性能、低延迟的异步 Socket 服务端 / 客户端 .NET 库,原生支持 TCP、SSL/TLS、UDP、Unix 域套接字、HTTP、HTTPS、WebSocket 等全协议栈,专门解决 10K 高并发连接问题,可集成基于 Fast Binary Encoding 的高级消息协议。
核心特性
- 跨平台:Windows / Linux /macOS
- 全异步 I/O 通信,线程安全
- 传输协议:TCP、SSL、UDP、UDP 组播、Unix Domain Socket
- Web 协议:HTTP、HTTPS、WebSocket、安全 WebSocket
- 内置 Swagger OpenAPI 交互式文档
- 集成 FBE(快速二进制编码)消息协议
- 支持多播、优雅断开、错误捕获、会话管理
2. 环境要求
- .NET 6.0+
- Windows:Visual Studio、Git、7-Zip
- Linux/macOS:Git、CMake、7-Zip
- 可选:Rider
3. 编译构建
# 拉取仓库 git clone https://github.com/chronoxor/NetCoreServer.git cd NetCoreServer # Linux/macOS cd build ./unix.sh # Windows cd build vs.bat
构建后在 release 目录生成:
- NetCoreServer.zip:核心库程序集
- Benchmarks.zip:基准测试
- Examples.zip:所有示例代码
4. 完整示例(中文可直接运行)
4.1 TCP 聊天服务器
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NetCoreServer;
namespace TcpChatServer
{
class ChatSession : TcpSession
{
public ChatSession(TcpServer server) : base(server) {}
protected override void OnConnected()
{
Console.WriteLine($"TCP 会话 {Id} 已连接");
SendAsync("欢迎进入 TCP 聊天室!输入 ! 断开连接");
}
protected override void OnDisconnected()
{
Console.WriteLine($"TCP 会话 {Id} 已断开");
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
string msg = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
Console.WriteLine($"收到 {Id}:{msg}");
Server.Multicast(msg);
if (msg == "!") Disconnect();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"会话 {Id} 错误:{error}");
}
}
class ChatServer : TcpServer
{
public ChatServer(IPAddress address, int port) : base(address, port) {}
protected override TcpSession CreateSession() => new ChatSession(this);
}
class Program
{
static void Main()
{
var server = new ChatServer(IPAddress.Any, 1111);
server.Start();
Console.WriteLine("TCP 服务已启动,端口 1111");
Console.ReadLine();
server.Stop();
}
}
}
4.2 SSL 加密聊天服务器
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using NetCoreServer;
namespace SslChatServer
{
class ChatSession : SslSession
{
public ChatSession(SslServer server) : base(server) {}
protected override void OnHandshaked() => SendAsync("SSL 加密连接成功");
}
class ChatServer : SslServer
{
public ChatServer(SslContext context, IPAddress address, int port) : base(context, address, port) {}
protected override SslSession CreateSession() => new ChatSession(this);
}
class Program
{
static void Main()
{
var cert = new X509Certificate2("server.pfx", "qwerty");
var context = new SslContext(SslProtocols.Tls12, cert);
var server = new ChatServer(context, IPAddress.Any, 2222);
server.Start();
Console.ReadLine();
server.Stop();
}
}
}
4.3 UDP 回声服务器
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NetCoreServer;
namespace UdpEchoServer
{
class EchoServer : UdpServer
{
public EchoServer(IPAddress address, int port) : base(address, port) {}
protected override void OnStarted() => ReceiveAsync();
protected override void OnReceived(EndPoint ep, byte[] buf, long off, long size)
{
string msg = Encoding.UTF8.GetString(buf, (int)off, (int)size);
Console.WriteLine($"UDP {ep}: {msg}");
SendAsync(ep, buf, off, size);
}
}
class Program { static void Main() => new EchoServer(IPAddress.Any, 3333).Start(); }
}
4.4 HTTP 缓存服务器
using System;
using System.Collections.Concurrent;
using System.Net;
using NetCoreServer;
namespace HttpServer
{
static class Cache
{
public static ConcurrentDictionary<string, string> Data = new();
}
class HttpSession : HttpSession
{
public HttpSession(HttpServer server) : base(server) {}
protected override void OnReceivedRequest(HttpRequest req)
{
if (req.Method == "GET")
SendResponseAsync(Response.MakeGetResponse("Hello NetCoreServer"));
else if (req.Method == "POST")
{
Cache.Data[req.Url] = req.Body;
SendResponseAsync(Response.MakeOkResponse());
}
}
}
class Program
{
static void Main()
{
var s = new HttpServer(IPAddress.Any, 8080);
s.Start();
Console.ReadLine();
s.Stop();
}
}
}
4.5 WebSocket 聊天服务器
using System;
using System.Net;
using System.Text;
using NetCoreServer;
namespace WsChatServer
{
class ChatSession : WsSession
{
public ChatSession(WsServer server) : base(server) {}
public override void OnWsConnected(HttpRequest req) => SendTextAsync("欢迎 WebSocket 聊天室");
public override void OnWsReceived(byte[] buf, long off, long size)
{
string msg = Encoding.UTF8.GetString(buf, (int)off, (int)size);
((WsServer)Server).MulticastText(msg);
}
}
class ChatServer : WsServer
{
public ChatServer(IPAddress a, int p) : base(a, p) {}
protected override WsSession CreateSession() => new ChatSession(this);
}
class Program { static void Main() => new ChatServer(IPAddress.Any, 8080).Start(); }
}
5. 性能基准(官方实测)
往返测试(10 秒,32 字节消息)
| 协议 | 客户端数 | 吞吐量(msg/s) | 延迟 |
|---|---|---|---|
| TCP Echo | 1 | 941 万 | 106ns |
| SSL Echo | 1 | 462 万 | 216ns |
| UDP Echo | 1 | 11 万 | 9μs |
| WebSocket | 1 | 113 万 | 878ns |
多播测试
- TCP 多播:约 133 万 msg/s
- SSL 多播:约 106 万 msg/s
- WebSocket 多播:约 60 万 msg/s
本文来自博客园,作者:南風未起,转载请注明原文链接:https://www.cnblogs.com/Andy-Blog/p/19923599

浙公网安备 33010602011771号