To recommend a lightweight, plug-in support for a comprehensive network communications library : TouchSocket.
TouchSocket's basic communication features include TCP, UDP, SSL, RPC, and HTTP, where the HTTP server supports extensions such as WebSocket, Static Web Pages, XML-RPC, WebAPI, and JSON-RPC.
In addition, TouchSocket supports TouchRPC with custom protocols, SSL encryption, asynchronous calls, rights management, error status returns, service callbacks and distributed calls.
The 100,000 calls take only 3.8 seconds when the function is empty and 0.9 seconds when it does not return a state.
TouchSocket (Pro) is a family of .NET-based assemblies for C#, F# and projects corresponding to the . Whether your project is Console, WinForms, WPF or Core, it is fully supported.
TouchSocket(Pro) has long supported .NET 4.5, .NET 4.8.1, and .NET Standard 2.0, with .NET 6.0 as the latest stable version support platform and .NET 7.0 as the latest release platform.
Project environment
NET 4.5: Guaranteed minimum supported version on . Basically 99% of the features of the whole system are supported.
NET 4.8.1: This is on top of the net45 dependencies, with some additional official Microsoft libraries added to support reaching net6.0-like functionality (e.g. Span, ValueTuple, ValueTask, etc.).
NET Standard 2.0: this is a guaranteed minimum dependency on a number of common platforms. It basically supports 99% of the features of the whole system.
NET 6.0: This is the latest stable release platform available, which also guarantees full functionality with minimal dependencies.
NET 7.0: this is the latest release platform at the moment, and we will be developing the most tastable features (e.g., AOT, etc.) on this platform.
Supported frameworks for frameworks Console, WPF, Winform, Blazor Server, Xamarin, MAUI, Avalonia, Mono, Unity 3D (except WebGL), others (i.e., all C# families)
Project Features
1. Function guide
2、Project Documentation
Project Characteristics
1、Traditional IOCP and TouchSocket's IOCP Mode
TouchSocket's IOCP mode is different from traditional IOCP. Taking Microsoft's official example, traditional IOCP uses MemoryBuffer to open up a block of memory, divides it equally, allocates an area for each session to receive data, and then copies and processes the data when it receives it. TouchSocket, on the other hand, obtains a block of available memory from the memory pool before each reception and uses it directly for reception, and then processes the block directly after receiving the data, thus avoiding the copying operation. Although this is a minor design difference, it results in a 10x performance improvement at 100,000 transfers of 64KB data.
2. Data processing adapters
TouchSocket was designed with the best ideas from other products, and the data processing adapter is one of them. Unlike other products, TouchSocket's adapter is more powerful, easy to use and flexible. It can not only parse packets in advance, but also parse data objects, and can be replaced at any time with immediate effect. For example, it can use fixed packet headers to pre-process data, thus solving the problem of packet splitting and sticky packets; it can also directly parse HTTP data protocols and WebSocket data protocols, and so on.
3、Compatibility and Adaptation
TouchSocket offers a variety of framework models that are fully compatible with all TCP and UDP based protocols. For example, TcpService and TcpClient have the same basic functionality as Socket, but with enhanced frame robustness and concurrency, throwing connections and receiving data through events, making them more user-friendly.
Project use
1. Nuget Installation
Enter TouchSocket in the search box, then select it from the search results and click Install, as shown below
2、TcpService
TcpService is the base class of the Tcp system server, it does not participate in the actual data interaction, but only configures, activates, manages, logs off, and rebuilds instances of the SocketClient class.
And SocketClient is when theTcpClient After successfully connecting to the server, a new instance class is created by the server, and all subsequent communication is done through this instance.
var service = new TcpService(); = (client, e) => { return ; };//A client is connecting. = (client, e) => { return ; };//A client has successfully connected = (client, e) => { return ; };//A client is disconnecting, valid only when actively disconnecting. = (client, e) => { return ; };//A client disconnects = (client, e) => { //Receiving information from the client var mes = Encoding.(, 0, );//Note: The data length is ($"Information has been received from {}: {mes}"); (mes);//Returns the received message directly to the sender //("id",mes);//Returns the received information to the client with the specific ID //Returns the received message to all clients online. //Note: This is just a suggested idea, in fact, mass mailing should be designed using the producer-consumer pattern //var ids = (); //foreach (var clientId in ids) //{ // if (clientId != )//Not for myself. // { // (clientId, mes); // } //} return ; }; (new TouchSocketConfig()//Load Configuration .SetListenIPHosts("tcp://127.0.0.1:7789", 7790)//Listening to two addresses at the same time .ConfigureContainer(a =>//The container should be configured at the top of the order { ();//Add a console log injection (note: console logging is not available in maui) }) .ConfigurePlugins(a => { //();//Plug-ins can be added here })); ();//activate (a plan)
3、TcpClient
TcpClient is the client base class of Tcp system, he is directly involved in tcp connection, sending, receiving, processing, disconnecting, etc., his business and the server's SocketClient is a one-to-one correspondence.
var tcpClient = new TcpClient(); = (client, e) => { return ; };//About to connect to the server, a socket has been created, but no tcp has been established. = (client, e) => {return ; };//Successful connection to the server = (client, e) => {return ; };//About to disconnect from the server. This is only valid for active disconnections. = (client, e) => {return ; };//Disconnects from the server and does not trigger when the connection is unsuccessful. = (client, e) => { //Receive information from the server. But generally byteBlock and requestInfo will present different values depending on the adapter. var mes = Encoding.(, 0, ); ($"The client receives the message: {mes}"); return ; }; //Load Configuration (new TouchSocketConfig() .SetRemoteIPHost("127.0.0.1:7789") .ConfigureContainer(a => { ();//Add a log injection })); ();//Calls the connection and throws an exception when the connection is unsuccessful. //Result result = ();//Or you can call TryConnect //if (()) //{ //} ("Client successfully connected"); ("RRQM");
4、TcpClient Disconnect and Reconnect
Just use the reconnect plugin in the plugin configuration in Config.
.ConfigurePlugins(a=> { (5, true, 1000);//To try to connect forever, tryCount is set to -1. });
Project Cases
1. Engineer's Software Toolbox
An in-house software toolkit for engineers developed using TouchRpc .
2、Remote monitoring and control projects
Project Address
GitHub:/RRQM/TouchSocket
Gitee:/RRQM_Home/TouchSocket
Document Address:
If you found this article helpful, why not support it with a like? Your support is what keeps me motivated to continue sharing my knowledge. Feel free to leave a comment if you have any questions or need further help.
You can also join WeChat[DotNet Technician] community to share ideas and grow with other tech-loving peers!Excellence is a habit, so feel free to leave a comment and learn!