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);
}
}
///
///receiving complete event
///
///
///
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);
}
}
///
///receiving complete event
///
///
///
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);
}
}
///
///send complete event
///
///
///
Private static void SendComplete (Network e, SocketParas sock)
{
}
///
///exit event
///
///
///
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;
}
}
CodePudding user response:
The type of XXSocket much better ah,CodePudding user response:
I've more or less contact with some..Is it has to have at least three callback?
Oncon (socket client)
Onmessage (socket client, byte [] data)
Onclose (socket client)
It seems you only the second..
In addition you exit, no client information ah.. At least I also get from List
And you describe the