Home > Backend Development > C#.Net Tutorial > How to implement simulated login code sharing in networkcomms3.0 in C# (picture)

How to implement simulated login code sharing in networkcomms3.0 in C# (picture)

黄舟
Release: 2017-06-18 10:16:18
Original
2401 people have browsed it

This article mainly introduces the summary of simulated login in C# networkcomms 3.0. Friends who need it can refer to it

Recent projects need to be a customerQueryStatus system, the current host computer lacks services function, so I found the networkcomms open source framework and used it as a project.

The latest version of networkcomms download address:https://github.com/MarcFletcher/NetworkComms.Net

Download directly vs open

New server


##

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)
    {
      //服务器开始监听客户端的请求
      Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)));
      //服务器开始监听客户端的请求      
      //IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));
      //TCPConnection.StartListening(thePoint, false);
      button1.Text = "监听中";
      button1.Enabled = false;
      //button1.Text = "监听中";
      //button1.Enabled = false;
      //此方法中包含服务器具体的处理方法。
      StartListening();
    }
    private void StartListening()
    {
      //开启日志记录 
      //配置日志记录器
      //ILogger logger = new LiteLogger(LiteLogger.LogMode.ConsoleAndLogFile, "ServerLogFile_" + NetworkComms.NetworkIdentifier + ".txt");
      //NetworkComms.EnableLogging(logger);
      //禁用日志记录 服务器端正式使用时,赢禁用日志记录
      NetworkComms.DisableLogging();
      //服务器端处理收到的消息
      //为简单起见,此示例中我们只处理字符类型的信息,也返回字符类型的信息。
      //处理的信息可以使自定义类,具体见下一个Demo
      NetworkComms.AppendGlobalIncomingPacketHandler<LoginContract>("ReqLogin", IncomingLoginRequest);
    }
    //处理某个具体的请求
    private void IncomingLoginRequest(PacketHeader header, Connection connection, LoginContract loginContract)
    {
      try
      {
        string resMsg = "";
        //为了简单,这里不调用数据库,而是模拟一下登录
        if (loginContract.UserID == "1000" && loginContract.PassWord == "123")
          resMsg = "登录成功";
        else
          resMsg = "用户名密码错误";
        //把返回结果写入到契约类中,后面返回给客户端
        //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");
      }
    }
  }
}
Copy after login

often in other help Missing this line: This leads to problems with type packaging when the client sends. This line of code must be added on both sides of the client and server to specify the transmission method


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

This is the error reported

This is 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()
    {
      InitializeComponent();
    }
    //连接信息对象
    public ConnectionInfo connInfo = null;
    //连接对象
    Connection newTcpConnection;
    SendReceiveOptions aboveOptions = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null);
    private void button1_Click(object sender, EventArgs e)
    {
      //给连接信息对象赋值
      connInfo = new ConnectionInfo(txtIP.Text, int.Parse(txtPort.Text));
      //如果不成功,会弹出异常信息
      newTcpConnection = TCPConnection.GetConnection(connInfo);
      button1.Enabled = false;
      button1.Text = "连接成功";
    }
    private void btnlogin_Click(object sender, EventArgs e)
    {
      //给契约类赋值
      LoginContract contract = new LoginContract(txtUserName.Text, txtPassword.Text);
      //contract.UserID = txtUserName.Text;
      //contract.PassWord = txtPassword.Text;
      //向服务器发送登录信息并获取登录结果
       ResMsgContract resMsg = newTcpConnection.SendReceiveObject("ReqLogin", "ResLogin", 5000, contract);
      //向服务器发送登录信息并获取登录结果
      // ResMsgContract resMsg = newTcpConnection.SendReceiveObject("ReqLogin", "ResLogin", 5000, contract);
      if (resMsg.Message == "登录成功")
      {
        MessageBox.Show("登录成功");
      }
      else
      {
        MessageBox.Show("用户名密码错误");
      }
    }
  }
}
Copy after login

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;
    }
  }
}
Copy after login

Note:When using this framework, you must cooperate with Google’s protobuf and choose a good version .I have not repeatedly tested the highest version, because when other problems occurred during the debugging and login process, I also changed the protobuf version. So far, I have not tested whether the highest version has compatibility issues. I have successfully used 2.0.0.668

Introduction to protobuf protobuf is an open source serialization framework provided by Google. It is similar to data representation languages ​​​​such as XML and JSON. Its biggest feature is that it is based on binary, so it is more efficient and shorter than traditional XML representation


vs nuget adding method

Input

Version select and specify it yourself, increase the contract class of the project Inside. This is the way to define the transmission object yourself.

Result:

The above is given by the editor The summary of simulated login implementation in C# networkcomms 3.0 introduced by you is hoped to be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the Script House website!

The above is the detailed content of How to implement simulated login code sharing in networkcomms3.0 in C# (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template