C# networkcomms 3.0实现模拟登陆总结

2019-12-30 18:04:11于海丽

最近项目需要做一个客户查询状态系统,当前上位机缺少服务功能,于是找到了networkcomms 开源框架,作为项目使用.

最新版networkcomms 下载地址:#25af49f0e79fce90312f328a2c7bc2cc#

下载直接vs打开

新建服务器端

c#,networkcomms,3.0实现模拟登陆


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