10 Star 57 Fork 558

dotNET China / TouchSocket

forked from 若汝棋茗 / TouchSocket 
Create your Gitee Account
Explore and code with more than 8 million developers,Free private repositories !:)
Sign up
Clone or Download
Sync branch
Notice: Creating folder will generate an empty file .keep, because not support in Git



English | 中文


【Open source version】

Nuget Url Describe
NuGet version (TouchSocket) Gitee
TouchSocket is a lightweight, comprehensive network communication library that supports plug-in.
Basic communication functions include TCP, UDP, SSL, RPC, HTTP, etc. Among them, HTTP
server supports extended plugins such as WebSocket, Static web pages, XMLRPC, Webapi, JSONRPC
. Touchrpc with a custom protocol, support SSL encryption, asynchronous calls,
permissions management, error status return, service recovery, distributed calls, etc. When the empty load function
executes, 100,000 calls are only 3.8 seconds, and only 0.9 seconds when the state is not returned.
NuGet version (TouchSocket.AspNetCore) Gitee
TouchSocket.AspNetCore is an exclusive version suitable for Aspnetcore.

【Enterprise version】

Nuget Url Describe
NuGet version (TouchSocketPro) Gitee
TouchSocketpro is the enterprise version of TouchSocket, which is based on the original.
There are also some corporate versions. For details, please see Enterprise Edition Related
NuGet version (TouchSocketPro.AspNetCore) Gitee
TouchSocketpro.aspnetcore is an exclusive version suitable for Aspnetcore.

🖥Support environment

  • .NET Framework4.5 Above。
  • .NET Core3.1 Above。
  • .NET Standard2.0 Above。

🥪Support framework

  • Console
  • WPF
  • Winform
  • Blazor Server
  • Xamarin
  • MAUI
  • Avalonia
  • Mono
  • Unity 3D(Except webgl)
  • Others (that is, all C#)

🌴TouchSocket Features Speed

IOCP mode of traditional IOCP and TouchSocket

TouchSocket's IOCP is not the same as tradition. Take the official example of Microsoft as an example. Receive data,Then process the copy of the data. And TouchSocket takes a available memory block from the memory pool each time before receiving, and then is directly used to receive. After receiving the data, it will directly throw this memory block and process it. Operation, although it is only a small design, when the data of the transmission 10W times 64KB, the performance is different 10 times

Data handle adapter

I believe that everyone has used other socket products, so TouchSocket also borrows the excellent design concept of other products during design. Data processing adapter is one of them, but unlike the design of other products, TouchSocket's adapter functions are more powerful. Easy to use and flexible. It can not only analyze the data packet in advance, but also parse the data object, which can be replaced at any time, and then take effect immediately. For example: the data can be pre -processed using a fixed Baotou to solve the problem of data subcontracting and sticky bags. You can also directly analyze the http data protocol, WebSocket data protocol, etc.

Compatibility and adaptation

TouchSocket offers a variety of framework models that can be fully compatible with all protocols in the TCP and UDP protocols. For example: TCPSERVICE and TCPClient, their basic functions are exactly the same as sockets, but they only enhance the framework of the framework and concurrent and connect and to receive data. Out, allowing users to use more friendly use.

🔗Contact the author

🌟Explanation document

👑Functional cousin


✨Simple example

The following only examples are created in the simplest way. For more details, please see Explanation Document


TcpService service = new TcpService();
service.Connecting = (client, e) => { };//Some client is connecting
service.Connected = (client, e) => { };//There is a client connection
service.Disconnected = (client, e) => { };//There is a client that is cut off and connected
service.Received = (client, byteBlock, requestInfo) =>
    string mes = byteBlock.ToString();
    Console.WriteLine($"Receive information from {client.id}:{mes}");

    client.Send(mes);//Return the received information directly to the sender

    //client.Send("id",mes);//Return the received information to the client with a specific ID

    var clients = service.GetClients();
    foreach (var targetClient in clients)//Return the received information to all the clients online。
        if (targetClient.ID != client.ID)

service.Setup(new TouchSocketConfig()//Load
    .SetListenIPHosts(new IPHost[] { new IPHost(""), new IPHost(7790) })//At the same time, listen to two addresses
    .Start();//start up


TcpClient tcpClient = new TcpClient();
tcpClient.Connected = (client, e) => { };//Successfully connect to the server
tcpClient.Disconnected = (client, e) => { };//The connection is disconnected from the server, and it will not be triggered when the connection is unsuccessful.
tcpClient.Received = (client, byteBlock, requestInfo) =>
    //Receive information from the server
    string mes = Encoding.UTF8.GetString(byteBlock.Buffer, 0, byteBlock.Len);
    Console.WriteLine($"Receive information: {MES}");

//Declaration configuration
TouchSocketConfig config = new TouchSocketConfig();
config.SetRemoteIPHost(new IPHost(""))
    .SetBufferLength(1024 * 10);


【TcpClient Break】 In the config plug -in configuration, you must first enable the plug -in, and then use the re -connected plug -in.

   a.UseReconnection(5, true, 1000);

【FixedHeaderPackageAdapter Pack】

This adapter mainly solves the problem of TCP adhesion package. The data format adopts a simple and efficient "Baotou+Data Body" mode. Among them, Baotou supports:

  • Byte mode (1+n), one -time receives a maximum of 255 bytes.
  • USHORT mode (2+n), maximum receiving 65535 bytes.
  • INT mode (4+n), a maximum receiving 2G data at a time.

The above data header adopts the default end mode (small end mode) of TouchSocketbitConverter. Users can switch the default end mode according to the requirements.

TouchSocketBitConverter.DefaultEndianType = EndianType.Little;


Users customize fixed Baotou adapters mainly help users solve data frame information with fixed Baotou. For example: The following data format needs to be implemented with only a few interfaces to complete the analysis. For detailed operations, please refer to the API.



Users customize non -fixed Baotou adapters mainly help users solve the data frame information with non -fixed Baotou. For example: the most typical HTTP packet, the data head and the data body are separated by "\r\n", and the data header is not fixed due to the different request information of the request request, and the length of the data body is not fixed, and the length of the data body is It is also specified by the value display of the data head, so you can consider using the CustomunfixedHeaderDatahandlingAdapter analysis, which can be achieved only through simple development.

Thank you

Thank you for your support for TouchSocket. If you have any other questions, please submit it, or add a group of QQ: 234762506 discussion.

Thanks for the support of the following tools

Support author

Support entrance


TouchSocket是.Net(包括 C# 、VB.Net、F#)的一个整合性的socket网络通信框架。包含了 tcp、udp、ssl、http、websocket、rpc、jsonrpc、webapi、xmlrpc等一系列的通信模块。一键式解决 tcp 黏分包问题,udp大数据包分片组合问题等。使用协议模板,可快速实现「固定包头」、「固定长度」、「区间字符」等一系列的数据报文解析。 expand collapse


No release




Load More
can not load any more