Networkcomms.net official website of the network communication framework from the UK www.networkcomms.net Chinese website www.networkcomms.cn
The client sends a message to the server, and the server calculates the results back to the client, which is a common usage scenario in a network communication application.
Take the user login for example, the client sends the contract class containing the user name and password to the server, the server compares the data obtained with the received contract data after the database obtains the data, and if it is consistent, returns the login success information, if inconsistent, returns the unsuccessful login information
The NETWORKCOMMS framework supports synchronous invocation of messages, just like calling local methods in general.
Examples (using Networkcomms v3 syntax in this example):
User Login
Contract Class 1 The UserInfo is used to store the user name and password and pass it to the server side
[Protocontract] Public classUserInfo {[Protomember (1)] Public stringUserID; [Protomember (2)] Public stringPassword; PublicUserInfo () {} PublicUserInfo (stringUseridstringpassword) { This. UserID =UserID; This. Password =password; } }
Contract Class 2 Resmessage for storing messages returned to clients by the server
[Protocontract] Public class Resmessage { [Protomember (1)] publicstring Message; Public resmessage () {} Public Resmessage (string message) { this. message = message; } }
Client:
//Declare a contract class
UserInfo UserInfo =NewUserInfo (); //assign a value to the user ID of the contract classUserinfo.userid =TxtUserID.Text.Trim (); //Assigning a user password to a contract classUserinfo.password =TxtPassword.Text.Trim (); //Send the UserInfo contract class message to the server side and get the contract class message of the returned Resmessage type Resmessage logincontract= Newtcpconnection.sendreceiveobject<userinfo, resmessage> ("Userlogin","Resuserlogin",8000, UserInfo);
//The following is how to get the results returned to the server
if (logincontract.message== "Login succeeded")
MessageBox.Show ("Login successful, jump to next page");
Else
MessageBox.Show ("User name or password error");
Let's summarize the format of the synchronization method call above
The return contract class type logincontract=newtcpconnectin.sendreceiveobject< the type of contract class passed, the contract class type returned by > (" message Type", " Message return type ", Time-out, contract class instance )= Newtcpconnection.sendreceiveobject<userinfo, resmessage> ( " Userlogin " " Resuserlogin " 8000, userInfo);
Let's take a look at the server-side code.
Networkcomms.appendglobalincomingpackethandler<userinfo> ("userlogin", Incomingloginhandler);
Specific methods of treatment
//The third parameter is the actual contract class type that the client sends, and the NETWORKCOMMS communication framework automatically parses the binary data into this type of data
Private voidIncomingloginhandler (packetheader header, Connection Connection, UserInfo UserInfo) {Try {
Declares the instance of the contract class returned
Resmessage resmessage=new resmessage (); stringUserID =Userinfo.userid; stringPassWord =Userinfo.password; //Verify the user name and password from the database if(validation succeeded)
//Assign a value to the Contract class instance resmessage.message="Login Successful"; ElseResmessage.message="User name password error"; //The validation results are returned to the client connection. SendObject ("Resuserlogin", Resmessage); } Catch(Exception ex) {logtools.logexception (ex,"Incomingloginhandler"); } }
C # network communication framework Networkcomms kernel parsing three-message synchronous call