introduced
- TCP components are the most basic components based on the TCP protocol. Their basic functions are the same as Socket. However, after being encapsulated by RRQM, a series of basic functions such as high connectivity, high concurrency, and data processing are packaged, so that users no longer care about infrastructure construction and concentrate on business.
- In theory, TCP components can be used in any product based on the TCP protocol, such as HTTP, FTP, WebSocket, Telnet, PLC communication, upper computer communication, etc.
product features
- Easy to use.
- Multithreaded.
- memory pool
- High performance (the server can receive 200w messages per second, and receive data traffic up to 2.5GB/s)
- Multiple data reception modes (IOCP, BIO, Select).
- Multi-address monitoring (you can monitor multiple IPs and ports at once)
- Adapter preprocessing, one-click solutions to subcontracting, sticking, object parsing (such as HTTP, Json), etc.
- Ultra-simple synchronous send, asynchronous send, receive and other operations.
- Based on event-driven, every step of the operation is under control.
Product application scenarios
- Basic TCP usage scenarios: It can be used across platforms and languages.
- Custom protocol parsing scenario: TCP data packets in any data format can be parsed.
Here is a demonstration of our system:
Create TcpService
TcpService is the base class of TCP servers, but it does not participate in the actual data interaction. The actual data interaction is completed by SocketClient, so the functions of TcpService are only configuration, activation, management, logout,
Rebuild an instance of the SocketClient class, so in TcpService, you must specify its SocketClient derived generic type, and then you must implement the HandleReceivedData method, which instructs how to handle received data or objects converted by the adapter.
So the specific creation process is as follows.
TcpService service = new TcpService();
service.Connecting += (client, e) =>{};//有客户端正在连接
service.Connected += (client, e) =>{};//有客户端连接
service.Disconnected += (client, e) =>{};//有客户端断开连接
service.Received += (client, byteBlock ,requestInfo) =>
{
//从客户端收到信息
string mes = Encoding.UTF8.GetString(byteBlock.Buffer, 0, byteBlock.Len);
Console.WriteLine($"已从{client.Name}接收到信息:{mes}");//Name即IP+Port
};
//声明配置
var config = new TcpServiceConfig();
config.ListenIPHosts = new IPHost[] { new IPHost("127.0.0.1:7789"), new IPHost(7790) };//同时监听两个地址
//载入配置
service.Setup(config);
service.Start();
Create TcpClient
TcpClient is the base class of TCP clients. It is an abstract class and cannot create instances. It must implement the HandleReceivedData method through inheritance, which indicates how to process the received data.
SimpleTcpClient tcpClient = new SimpleTcpClient();
tcpClient.Connected += (client, e) =>{};//成功连接到服务器
tcpClient.Disconnected += (client, e) =>{};//从服务器断开连接,当连接不成功时不会触发。
tcpClient.Received += (client, byteBlock ,requestInfo) =>
{
//从服务器收到信息
string mes = Encoding.UTF8.GetString(byteBlock.Buffer, 0, byteBlock.Len);
Console.WriteLine($"接收到信息:{mes}");
};
//载入配置
tcpClient.Setup("127.0.0.1:7789");
tcpClient.Connect();
tcpClient.Send(Encoding.UTF8.GetBytes("RRQM"));
Both client-side and server-side send are encapsulated with the send method. TcpClient and TcpService already have multiple send methods built in, and you can send them by calling them directly. If the send fails, an exception is thrown immediately.
service.Send(“”);

Finally, if you like my article, please pay attention to it. I hope that the net ecosystem will get better and better!