Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand socket tcp package MiniSocket

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to understand the socket tcp development package MiniSocket, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

MiniSocket uses socket tcp to communicate, automatically handles subcontracting and merging, has high performance, is easy to use, and is very easy to carry out secondary development.

Server use: open vs2017 new .net core (version 2.1 or above) console application, nuget search MiniSocket download

Code:

Static void Main (string [] args)

{

/ / actual server object

Network server = new Network ()

/ / set the ip address

Server.Ip = "127.0.0.1"

/ / set the port number

Server.Port = 9123

/ / set the maximum number of client connections

Server.ClientMaxCnt = 10

/ / set the receive timeout

Server.ReceiveTimeout = 1000 * 100

/ / set send timeout

Server.SendTimeout = 1000 * 100

/ / set the receive buffer size

Server.RecvBufferSize = 1024

/ / set the number of worker threads

Server.WorkThreadCnt = 20

/ / set client timeout (seconds)

Server.TimeOver = 30

/ / set to receive the completion event

Server.RecvCompleted + = RecvEventData

/ / initialize

Bool bl = server.Init ()

While (true)

{

Thread.Sleep (1000)

}

}

/ / /

/ / receive completion event

/ / /

/ / /

/ / /

Private static void RecvEventData (Network ejia SocketParas sockArgs)

{

Try

{

String str_head = Encoding.UTF8.GetString (sockArgs.HeadByt)

String str_body = Encoding.UTF8.GetString (sockArgs.BodyByt)

E.Send (sockArgs, Encoding.UTF8.GetBytes ("this is header"), Encoding.UTF8.GetBytes ("this is body"))

}

Catch (Exception ex)

{

Print ("RecvEventData" + ex.Message)

}

}

Client use: open vs2017 new .net framework (version 4.0 or above) console application, nuget search MiniSocketClient download

Code:

Static void Main (string [] args)

{

/ / instantiate the client object

Network client = new Network ()

/ / set the ip address

Client.Ip = "127.0.0.1"

/ / set the port number

Client.Port = 9123

/ / set the receive timeout

Client.ReceiveTimeout = 1000 * 30

/ / set send timeout

Client.SendTimeout = 1000 * 30

/ / set the receive buffer size

Client.ReceiveBufferSize = 1024

/ / receive completion event

Client.RecvCompleted + = RecvComplete

/ / exit event

Client.ExitCompleted + = ExitCompleted

/ / initialize

Client.Init ()

/ / send the first packet of data

Client.Send (Encoding.UTF8.GetBytes ("hello china"), Encoding.UTF8.GetBytes ("hello world"))

While (true)

{

Thread.Sleep (10)

}

}

/ / /

/ / receive completion event

/ / /

/ / /

/ / /

Private static void RecvComplete (Network e, SocketParas sockArgs)

{

Try

{

Console.WriteLine (Encoding.UTF8.GetString (sockArgs.HeadByt) + Encoding.UTF8.GetString (sockArgs.BodyByt))

E.Send (Encoding.UTF8.GetBytes ("hello world"), Encoding.UTF8.GetBytes ("hello MiniSocket"))

}

Catch (Exception ex)

{

Print ("RecvComplete" + ex.Message)

}

}

/ / /

/ / send completion event

/ / /

/ / /

/ / /

Private static void SendComplete (Network e, SocketParas sock)

{

}

/ / /

/ / exit event

/ / /

/ / /

/ / /

Private static void ExitCompleted (int state, string msg)

{

Switch (state)

{

Case 0: / / normal

Break

Case 1: / / connection exception

Break

Case 2: / / send an exception

Break

Case 3: / / receiving exception

Break

Case 4: / / forced exit manually

Break

}

}

The above is how to understand the socket tcp development package MiniSocket, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report