C # networkcomms 3.0 implements simulated login summary,

Source: Internet
Author: User

C # networkcomms 3.0 implements simulated login summary,

Recently, a customer query status system is required for the project. The current PC lacks service functions, so the networkcomms open-source framework is found and used as a project.

Newest networkcomms: https://github.com/MarcFletcher/NetworkComms.Net

Download directly vs open

Create a server

Using MessageContract; using NetworkCommsDotNet. connections; using NetworkCommsDotNet. connections. TCP; using NetworkCommsDotNet. DPSBase; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. net; using System. text; using System. windows. forms; namespace AppServer {public partial class MaiForm: Form {public MaiForm () {InitializeComponent ();} SendReceiveOptions aboveOptions = new SendReceiveOptions (DPSManager. getDataSerializer <ProtobufSerializer> (), null, null); private void button1_Click (object sender, EventArgs e) {// The server starts to listen to the client's request Connection. startListening (ConnectionType. TCP, new IPEndPoint (IPAddress. parse (txtIP. text), int. parse (txtPort. text); // The server starts listening to client requests // IPEndPoint thePoi Nt = new IPEndPoint (IPAddress. parse (txtIP. text), int. parse (txtPort. text); // TCPConnection. startListening (thePoint, false); button1.Text = "listener"; button1.Enabled = false; // button1.Text = "listener"; // button1.Enabled = false; // This method contains the specific server processing method. StartListening ();} private void StartListening () {// Enable Logging // configure the logger // ILogger logger = new LiteLogger (LiteLogger. logMode. leleandlogfile, "ServerLogFile _" + NetworkComms. networkIdentifier + ". txt "); // NetworkComms. enableLogging (logger); // disable NetworkComms when the logging server is used properly. disableLogging (); // The server processes the received message // For the sake of simplicity. In this example, we only process the information of the character type and also return the information of the character type. // The information to be processed can be customized. For details, see the next Demo NetworkComms. appendGlobalIncomingPacketHandler <LoginContract> ("ReqLogin", IncomingLoginRequest);} // process a specific request private void IncomingLoginRequest (PacketHeader header, Connection connection, LoginContract loginContract) {try {string resMsg = ""; // For the sake of simplicity, we do not call the database here, But simulate the login if (loginContract. userID = "1000" & loginContract. passWord = "123") resMsg = "Logon successful"; else resMsg = "username and PassWord error"; // write the returned results to the Contract class, return to the client // ResMsgContract contract = new ResMsgContract (); // contract. message = resMsg; // connection. sendObject <ResMsgContract> ("ResLogin", contract); ResMsgContract contract = new ResMsgContract (); contract. message = resMsg; connection. sendObject ("ResLogin", contract);} catch (Exception ex) {// LogTools. logException (ex, "IncomingMsgHandle ");}}}}

 

This line of code is often missing in other help scenarios: as a result, a type packaging problem occurs when the client sends messages. This line of code is added at both ends of the client server and is used to specify the transmission mode.

 SendReceiveOptions aboveOptions = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null);

This is the error.

Here is the client.

Using MessageContract; using NetworkCommsDotNet. connections; using NetworkCommsDotNet. connections. TCP; using NetworkCommsDotNet. DPSBase; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace AppClient {public partial class MainForm: Form {public MainForm () {InitializeComponent () ;}// Connection information object public ConnectionInfo connInfo = null; // Connection object newTcpConnection; sendReceiveOptions aboveOptions = new SendReceiveOptions (DPSManager. getDataSerializer <ProtobufSerializer> (), null, null); private void button1_Click (object sender, EventArgs e) {// assign connInfo = new ConnectionInfo (txtIP. text, int. parse (txtPort. text); // if it fails, the exception message newTcpConnection = TCPConnection will pop up. getConnection (connInfo); button1.Enabled = false; button1.Text = "connection successful";} private void btnlogin_Click (object sender, EventArgs e) {// assign LoginContract contract = new LoginContract (txtUserName. text, txtPassword. text); // contract. userID = txtUserName. text; // contract. passWord = txtPassword. text; // send the logon information to the server and obtain the logon result ResMsgContract resMsg = newTcpConnection. sendReceiveObject <LoginContract, ResMsgContract> ("ReqLogin", "ResLogin", 5000, contract); // send logon information to the server and obtain logon results. // ResMsgContract resMsg = newTcpConnection. sendReceiveObject <ResMsgContract> ("ReqLogin", "ResLogin", 5000, contract); if (resMsg. message = "Login successful") {MessageBox. show ("Logon successful");} else {MessageBox. show ("username and password error ");}}}}

Contract type

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MessageContract{    [ProtoContract]    public class LoginContract    {        [ProtoMember(1)]        public string UserID { get; set; }        [ProtoMember(2)]        public string PassWord { get; set; }        public LoginContract() { }        public LoginContract(string userID, string passWord)        {            this.UserID = userID;            this.PassWord = passWord;        }    }}
using ProtoBuf;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MessageContract{    [ProtoContract]    public  class ResMsgContract    {        [ProtoMember(1)]        public string Message;        public ResMsgContract() { }        public ResMsgContract(string message)        {            this.Message = message;        }    }}

 

Note:

This framework should be used with Google's protobuf to select a proper version. I did not repeat the test of the highest version, because the protobuf version was changed when other problems occurred during the debugging and login process. So far, I have not tested whether the highest version is compatible. I successfully used 2.0.0.668.

Protobuf introduction protobuf is an open-source serialization framework provided by google. It is similar to a data representation language such as XML and JSON. Its biggest feature is that it is based on binary, so it is less efficient than the traditional XML Representation.

Vs nuget Addition Method

Input

Select your own version to add the contract class of the project. This is your own way to define the transmission object.

 

Result:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.