I.TCPSVR How to use A. Test procedure: using system; using ibms.net.tcpcsframework; using system.collections; using system.net.sockets;
Namespace ibms.test {///
Public static void main () {TRY {
Console.writeline ("Begin to Test TCPSVR Class ...");
TesttcpsVR TTS = New TesttcpsVR ();
// TCPSVR SVR = New TCPSVR (9050, 4); // Default Using ENCODING.DEFAULT Coding TCPSVR SVR = New TCPSVR (9050, 4, New CODER (CODER.EncodingMothOrd.utf8));
Svr.resovlver = New DataGramResolver ("##");
/ / Define 4 events of the server
// Server full svr.serverfull = new NetEvent (tts.serverfull);
// New client connection Svr.ClientConn = New NetEvent (TTS.CLIENTCONN);
// Client Close Svr.ClientClose = New NetEvent (TTS.ClientClose);
// Receive data SVR.Recvdata = new NetEvent (tts.recvdata); // Command Control Cycle While (TRUE) {Console.write (">");
String cmd = console.readline ();
// Exit the test program IF (cmd.tolower () == "exit") {Break;}
// Stop server program IF (cmd.tolower () == "stop") {svr.stop (); console.writeline ("Server is stop.");
CONTINUE;
// Run server program IF (cmd.tolower () == "start") {svr.start ();
Console.writeline ("Server IS Listen ... {0}", SVR.Serversocket.localendPoint.toString ());
CONTINUE;
// Look at the number of servers online clients and capacity IF (cmd.tolower () == "count") {Console.WriteLine ("Current Count of Client IS {0} / {1}", Svr.Sessioncount, Svr.capacity CONTINUE;
// Send data to the client format: send [session] [stringData] IF (cmd.tolower (). Indexof ("send")! = - 1) {cmd = cmd.tolower (); string [] para = cmd .Split ('');
IF (Para.Length == 3) {session client = (session) SVR.SessionTable [New sessionID (int.parse (PARA [1])];
IF (client! = null) {svr.send (Client, Para [2]);} else {console.writeline ("the session is null";}}}} else {console.writeline ("Error Command");}
CONTINUE;
// kick off a client if (cmd.tolower (). Indexof ("kick")! = - 1) {cmd = cmd.tolower ();
String [] Para = cmd.split (''); if (Para.length == 2) {session client = (session) SVR.SessionTable [New sessionID (INT.PARSE (PARA [1])];
IF (client! = null) {svr.closesis (client);} else {console.writeline ("the session is null);}} else {console.writeline (" Error Command ");
}
// List all client information IF on the server (cmd.tolower () == "list") {INT i = 0;
Foreach (session client in savr.sessionTable.values) {if (client! = null) {i ; string info = string.format ("{0} client: {1} Connected Server Session: {2}. socket handle: { 3} ", i, client.clientsocket.RemoteEndPoint.toString (), client.id, client.clientsocket.handle;
Console.writeLine (INFO);} else {i ;
String info = string.format ("{0} null client", i); console.writeline (info);
}
CONTINUE;
Console.writeline ("Unkown Command");
} // end of while
Console.writeline ("End Service");} catch (exception ex) {console.writeline (ex. scrod ());
}
Void ClientConn (Object Sender, NetEventargs E) {string info = string.format ("a client: {0} Connect Server Session: {1}. socket handle: {2}", E.CLIENT.CLIENTSOCKET.RemoteEndPoint.Tostring ( ), E.CLIENT.ID, E.Client.clientSocket.handle;
Console.writeLine (INFO);
Console.Write (">");} Void ServerFull (Object Sender, NetEventargs E) {string info = string.format ("Server is full.the client: {0} is refused", E.CLIENT.CLIENTSOCKET.RemoteEndPoint. TOSTRING ());
// must do it // server is full, you must turn off the new client connection E.CLIENT.CLOSE ();
Console.writeLine (INFO);
Console.write (">");
}
Void ClientClose (Object Sender, NetEventAndargs E) {string info;
IF (E.Client.TypeOfExit == session.exittype.exceptionExit) {info = string.format ("a client session: {0} exception closed.", ELSE {info = string.format ("A client session: {0} normal closed.", E.CLIENT.ID);
Console.writeLine (INFO);
Console.write (">");
Void Recvdata (Object Sender, NetEventAndargs E) {string info = string.format ("RECV DATA: {0} from: {1}.", E.CLIENT.DATAGRAM, E.CLIENT;
Console.writeline (INFO); TCPSVR SVR = (TCPSVR) Sender;
// Test the received data to the client Svr.send (E.CLIENT, E.CLIENT.DATAGRAM);
Console.write (">");
}
B. Description: Use the command to operate the server exit exit START Start Services Kick Close Close Close Close Close Close Close The Send Data List lists all client status count client count
Start the service to run Start, wait for the client connection. You can then use list, count to view the current connection status
Each event has a corresponding function. Customers handle their business logic in the event handler, can use their own server applications, the basic framework does not change the two .TCPCLI method A. Test program: using system; using IBMS.NET.TCPCSFRAMEWORK;
Summary description of Namespace IBMS.TEST {///
TesttcpClient test = new testtcpclient ();
TCPCli CLI = New TCPCLI (New Coder (CODER.EncodingMothOrd.utf8));
CLI.Resovlver = New DataGramResolver ("##");
CLI.ReceivedDataGram = New NetEvent (TEST.RECVDATA);
CLI.DisconnectedServer = New NetEvent (Test.ClientClose);
CLI.CONNECTEDSERVER = New NetEvent (Test.ClientConn); try {// Command Control Cycle While (TRUE) {Console.write (">");
String cmd = console.readline ();
IF (cmd.tolower () == "exit") {Break;}
IF (cmd.tolower () == "close") {cli.close ();
CONTINUE;
IF (cmd.tolower (). INDEXOF ("conn")! = - 1) {cmd = cmd.tolower ();
String [] Para = cmd.split ('');
IF (Para.Length == 3) {cli.connect (Para [1], INT.PARSE (Para [2]));} else {console.writeline ("Error Command");}
CONTINUE;
IF (cmd.tolower (). INDEXOF ("send")! = - 1) {cmd = cmd.tolower ();
String [] Para = cmd.split ('');
IF (Para.length == 2) {cli.send (Para [1]);
} Else {console.writeline ("Error Command");
CONTINUE;
Console.writeline ("Unkown Command");
} // end of while
Console.writeline ("End Service");} catch (exception ex) {console.writeline (ex. scrod ());}}
Void ClientConn (Object Sender, NetEventargs E) {string info = string.format ("a client: {0} Connect Server: {1}", E.CLIENT, E.CLIENTSTRINGET.RemoteEndPoint.Tostring ());
Console.writeLine (INFO);
Console.write (">");} void clientclose (Object sender, NetEventargs e) {string info;
IF (E.Client.TypeOfExit == session.exittype.exceptionExit) {info = string.format ("a client session: {0} exception closed.", ELSE {info = string.format ("A client session: {0} normal closed.", E.CLIENT.ID);
Console.writeLine (INFO);
Console.write (">");
Void Recvdata (Object Sender, NetEventAndargs E) {string info = string.format ("RECV DATA: {0} from: {1}.", E.CLIENT.DATAGRAM, E.CLIENT;
Console.writeline (Info); Console.Write (">");}}}
B. Description: First establish a connection, conn 192.9.207.214 9050 can then send the connection close to the connection
III. Encoder If you want to encrypt your packet, you need a yourself from inheriting from the CODER class, such as a MyCoder class, then overload the encoding and decoding function. Instructions:
TCPCLI CLI = New TCPCLI (New Mycoder ());
This encoder can be used on the client. IV. The packet parser is the same as the encoder.