Home > Net >  MiniSocket easy-to-use socket TCP development kit the.net core
MiniSocket easy-to-use socket TCP development kit the.net core

Time:09-30

MiniSocket using socket TCP communications, automatic processing of the subcontract to merge, high performance, simple to use, easy to secondary development

https://blog.csdn.net/weixin_45785782/article/details/102694934

CodePudding user response:

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

Code:

The static void Main (string [] args)
{
//actualization server object
The Network server=new Network ();
//set the IP address
Server Ip="127.0.0.1";
//set the port number
Server. The Port=9123;
//set the client maximum number of connections
Server. ClientMaxCnt=10;
//set to receive a timeout
Server. The ReceiveTimeout=1000 * 100;
//set to send timeout
Server. SendTimeout=1000 * 100;
//set the receive buffer size
Server. RecvBufferSize=1024;
//set the number of threads
Server. WorkThreadCnt=20;
//set the client timeout (in seconds)
Server. TimeOver=30;
//set to receive complete event
Server. RecvCompleted +=RecvEventData;
//initialize
Bool bl=server. The Init ();
While (true)
{
Thread.sleep (1000);
}
}

///& lt; Summary>
///receiving complete event
///& lt;/summary>
///& lt; Param name="e" & gt;
///& lt; Param name="sockArgs & gt;"
Private static void RecvEventData (Network e, SocketParas sockArgs)
{

Try
{
String str_head.=Encoding UTF8. Get string (sockArgs. HeadByt);
String str_body.=Encoding UTF8. Get string (sockArgs. BodyByt);

E.S end (sockArgs, Encoding UTF8 GetBytes (" this is the header "), Encoding, UTF8. GetBytes (" this is the body "));
}
The catch (Exception ex)
{
Print (" RecvEventData "+ ex. Message);
}
}

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

Code:

The static void Main (string [] args)
{
//instantiate the client object
The Network client=new Network ();
//set the IP address
Client. Ip="127.0.0.1";
//set the port number
Client. The Port=9123;
//set to receive a timeout
Client. The ReceiveTimeout=1000 * 30;
//set to send timeout
Client. SendTimeout=1000 * 30;
//set the receive buffer size
Client. The ReceiveBufferSize=1024;
//receive complete event
Client. RecvCompleted +=RecvComplete;
//exit event
Client. ExitCompleted +=ExitCompleted;
//initialize
Client. The Init ();
//send the first packet of data
Client. The Send (Encoding UTF8. GetBytes (" hello China "), Encoding, UTF8. GetBytes (" hello world "));

While (true)
{
Thread.sleep (10);
}

}



///& lt; Summary>
///receiving complete event
///& lt;/summary>
///& lt; Param name="e" & gt;
///& lt; Param name="sockArgs & gt;"
Private static void RecvComplete (Network e, SocketParas sockArgs)
{
Try
{

Console. WriteLine (Encoding UTF8. Get string (sockArgs. HeadByt) + Encoding. The UTF8. Get string (sockArgs. BodyByt));
E.S end (Encoding UTF8. GetBytes (" hello world "), Encoding, UTF8. GetBytes (" hello MiniSocket "));
}
The catch (Exception ex)
{
Print (" RecvComplete "+ ex. Message);
}
}

///& lt; Summary>
///send complete event
///& lt;/summary>
///& lt; Param name="e" & gt;
///& lt; Param name="sock" & gt;
Private static void SendComplete (Network e, SocketParas sock)
{

}

///& lt; Summary>
///exit event
///& lt;/summary>
///& lt; Param name="state" & gt;
///& lt; Param name="MSG" & gt;
Private static void ExitCompleted (int, string MSG)
{
The switch (state)
{
Normal case 0://
break;
Case 1://connection exception
break;
Case 2://send abnormal
break;
Case 3://receive abnormal
break;
Case 4://manual kicked out
break;
}
}
  •  Tags:  
  • C#
  • Related