networkcomms3.0 How to implement analog login code sharing in C #

Source: Internet
Author: User
This article mainly introduces the C # Networkcomms 3.0 implementation of the simulation landing summary, the need for friends can refer to the following

Recently the project needs to do a customer query status system, the current host computer lacks the service function, so found the Networkcomms open source framework, as a project use.

Latest Version Networkcomms download address: Https://github.com/MarcFletcher/NetworkComms.Net

Download Direct vs Open

New server-side


Using messagecontract;using networkcommsdotnet;using networkcommsdotnet.connections;using Networkcommsdotnet.connections.tcp;using networkcommsdotnet.dpsbase;using system;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) {//server started listening for client request connection.startlistening (Connection TYPE.TCP, New IPEndPoint (Ipaddress.parse (txtip.text), Int.      Parse (Txtport.text)); The server starts listening for client requests//ipendpoint Thepoint = new IPEndPoint (Ipaddress.parse (txtip.text), Int.      Parse (Txtport.text));      Tcpconnection.startlistening (Thepoint, false); Button1.Text = "in-listen"; Button1.      Enabled = false; Button1.      Text = "in-listen"; Button1.      Enabled = false;      This method contains server-specific processing methods.    Startlistening (); } private void Startlistening () {///Turn on logging//configuration logger//ilogger logger = new Litelogger (Litelogger.lo      Gmode.consoleandlogfile, "Serverlogfile_" + networkcomms.networkidentifier + ". txt");      Networkcomms.enablelogging (logger);      Disable logging when the server side is officially used, win disables logging networkcomms.disablelogging ();      Server-side processing of received messages//For simplicity, in this example we only deal with the character type information, and also return the character type information. The information processed can be customized to the class, specifically see the next demo networkcomms.appendglobalincomingpackethandler<logincontract> ("Reqlogin",    Incomingloginrequest); }//Handle a specific request private void Incomingloginrequest (Packetheader header, Connection Connection, Logincontract logincontr        Act) {try {string resmsg = ""; To be simple, do not call the database here, but simulate the login if (Logincontract.userid = = "" && Logincontract.password = = "123") re    SMSG = "Login Successful";    else resmsg = "User name password error";        Writes the result of the return to the contract class, back 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 is often missing from other help: There was a problem with the type packaging when the client was sent. This line of code is added on both sides of the client server, and is the specified transfer mode


Sendreceiveoptions aboveoptions = new Sendreceiveoptions (dpsmanager.getdataserializer<protobufserializer> (), NULL, NULL);

That's the error.

The client


Using messagecontract;using networkcommsdotnet;using networkcommsdotnet.connections;using Networkcommsdotnet.connections.tcp;using networkcommsdotnet.dpsbase;using system;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 () {Initiali    Zecomponent ();    }//Connection information Object public ConnectionInfo conninfo = null;    Connection object Connection newtcpconnection; Sendreceiveoptions aboveoptions = new Sendreceiveoptions (dpsmanager.getdataserializer<protobufserializer> (),    NULL, NULL); private void Button1_Click (object sender, EventArgs e) {//Assign a value to the connection information object conninfo = new ConnectionInfo (txtip.text Int.      Parse (Txtport.text));      If unsuccessful, the exception message pops up newtcpconnection = Tcpconnection.getconnection (Conninfo); Button1.      Enabled = false; Button1.    Text = "Connection succeeded"; } private void BTNlogin_click (object sender, EventArgs e) {//Assign a value to the contract class logincontract contract = new Logincontract (txtusername.te      XT, txtPassword.Text); Contract.      UserID = txtUsername.Text; Contract.      PassWord = txtPassword.Text; Send login information to the server and get login results resmsgcontract resmsg = newtcpconnection.sendreceiveobject<logincontract, resmsgcontract>      ("Reqlogin", "Reslogin", contract); Send login information to server and get login result//resmsgcontract resmsg = newtcpconnection.sendreceiveobject<resmsgcontract> ("ReqLogin",      "Reslogin", contract);      if (resmsg.message = = "Login Successful") {MessageBox.Show ("login Successful");      } else {MessageBox.Show ("User name password error"); }    }  }}

Contract class


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;}}}  

Attention:

Use this framework to match Google's protobuf to choose a good version. I did not repeat the highest version of the test, because during the debug logon process during the process of other problems, also changed the version of PROTOBUF, has not tested the highest version of whether there is compatibility problem. I successfully use the 2.0.0.668

Protobuf Introduction Protobuf is an open-source serialization framework provided by Google, similar to the data representation language of Xml,json, which is most characteristic of binary-based, and therefore less efficient than traditional XML representations

VS NuGet Add Way

Input

Version choose to specify, increase the project contract class inside. This is how you define the Transfer object.

Results:

The above is a small series to introduce you to the C # Networkcomms 3.0 to achieve the simulation landing summary, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

Related Article

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.