- Notifications
You must be signed in to change notification settings - Fork181
TouchSocket is an integrated .NET networking framework that includes modules for socket, TCP, UDP, SSL, named pipes, HTTP, WebSocket, RPC, and more. It offers a one-stop solution for TCP packet issues and enables quick implementation of custom data message parsing using protocol templates.
License
RRQM/TouchSocket
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
En |中文
纸上得来终觉浅,绝知此事要躬行。
TouchSocket is a simple, modern, and high-performance .NET networking framework, supporting C#, VB.NET, F#, and more.It helps you build powerful communication applications such asTCP / UDP / MQTT / WebSocket / SSL / HTTP / Modbus / RPC with ease.
The framework features a highly optimized IOCP/Socket implementation, robust memory pooling, a flexible data-adapter pipeline, and a rich plugin ecosystem including heartbeat, reconnection, SSL, RPC, and more.
- .NET Framework ≥4.6.2
- .NET Standard ≥2.0
- .NET ≥6.0
Console / WinForm / WPF / MAUI / Avalonia / Blazor / Xamarin / Unity (non-WebGL) / Mono / All C#-based platforms
TouchSocket provides a deeply optimized IOCP design.
| Implementation | Memory Handling | Performance Impact |
|---|---|---|
| Traditional IOCP (Microsoft sample) | Uses a fixed shared buffer; received data must becopied to another buffer | Extra copy → High overhead under load |
| TouchSocket IOCP | Allocates a fresh memory block from thememory pool for each receive | Zero extra copy → Significant performance gain |
In stress tests (100k messages × 64KB), TouchSocket achievedup to 10× faster throughput compared to the traditional model.
The data-adapter pipeline is one of TouchSocket’s core strengths:
- Parsesheaders and payloads
- Handlessticky packets & fragmentation
- Converts directly todata objects
- Hot-swappable adapters
- Built-in templates: fixed header, fixed length, terminator, HTTP, WebSocket, etc.
Adapters make protocol development clean, modular, and highly reusable.
TouchSocket’sPlugins system allows extending communication behavior across the entire lifecycle:
- Auto reconnection
- Heartbeat detection
- SSL validation
- Logging
- Authentication
- Custom data pipelines
All via:
.ConfigurePlugins(a=>{ ...});
TouchSocket maintains full compatibility with native Socket semantics while improving:
- Stability
- Concurrency and throughput
- Connection lifecycle management
- Exception handling
- Unified event model (Connected / Received / Closed)
You can migrate existing Socket code with minimal changes.
Across TCP, UDP, WebSocket, and others, TouchSocket exposes a consistent set of APIs:
client.ConnectAsync(...)client.SendAsync(...)client.Received+= ...
This ensures a low learning curve and fast development.
The framework is optimized with:
- ByteBlock reusable high-performance buffers
- MemoryPool
- Span / Memory
Ensuring minimal allocations and low GC pressure during heavy workloads.
The following examples show only the simplest usage. Refer to the documentation for more advanced scenarios.
TcpServiceservice=newTcpService();service.Connected=(client,e)=>EasyTask.CompletedTask;service.Closed=(client,e)=>EasyTask.CompletedTask;service.Received=(client,e)=>{stringstr=e.Memory.Span.ToString(Encoding.UTF8);Console.WriteLine($"Received:{str}");returnEasyTask.CompletedTask;};awaitservice.StartAsync(7789);
TcpClientclient=newTcpClient();client.Connected=(c,e)=>EasyTask.CompletedTask;client.Closed=(c,e)=>EasyTask.CompletedTask;client.Received=(c,e)=>{Console.WriteLine(e.Memory.Span.ToString());returnEasyTask.CompletedTask;};awaitclient.ConnectAsync("127.0.0.1:7789");awaitclient.SendAsync("Hello");
.ConfigurePlugins(a=>{a.UseReconnection<TcpClient>();});
Solves packet fragmentation & merging issues.
Supports:
- Byte = 1 + n (≤255B)
- Ushort = 2 + n (≤65535B)
- Int = 4 + n (≤2GB)
Endianness is configurable:
TouchSocketBitConverter.DefaultEndianType=EndianType.Little;
For fixed-header formats such as:
| 1 | 1 | 1 | ********** |For variable-header protocols such as HTTP:
- Header ends with
\r\n\r\n - Body length from
Content-Length
A small amount of code can build a complete parser.
- CSDN Blog
- Bilibili Videos
- Source Repositories
- QQ Group:234762506
Thank you all for supporting TouchSocket.If you have questions, feel free to submit an issue or join the QQ group.
Special thanks to:
- Visual Studio
- JetBrains
- Visual Studio Code
TouchSocket is a member of thedotNET China organization.
About
TouchSocket is an integrated .NET networking framework that includes modules for socket, TCP, UDP, SSL, named pipes, HTTP, WebSocket, RPC, and more. It offers a one-stop solution for TCP packet issues and enables quick implementation of custom data message parsing using protocol templates.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.


