码迷,mamicode.com
首页 > Web开发 > 详细

NetworkComms V3 模拟登陆

时间:2015-03-30 20:57:11      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

演示NetworkComms V3的用法

例子很简单 界面如下:

技术分享

服务器端代码:

开始监听:

 //服务器开始监听客户端的请求
            Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)));
            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);
            }
            catch (Exception ex)
            {
                LogTools.LogException(ex, "IncomingMsgHandle");
            }
        }

使用protobuf.net 进行序列化

 契约类:

using System;
using System.Collections.Generic;
using System.Text;
using ProtoBuf;
 
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 System;
using System.Collections.Generic;
using System.Text;
using ProtoBuf; 
 
namespace MessageContract
{
    //操作结果的返回信息,可通用
    [ProtoContract]
    public class ResMsgContract
    {
 
        [ProtoMember(1)]
        public string Message;
 
        public ResMsgContract() { }
 
        public ResMsgContract(string message)
        {
            this.Message = message;
        }
    }
}

客户端代码:

//连接信息对象
        public ConnectionInfo connInfo = null;
        //连接对象
        Connection newTcpConnection;

连接服务器:

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 button2_Click(object sender, EventArgs e)
        {
            //给契约类赋值
            LoginContract contract = new LoginContract();
            contract.UserID = txtUserName.Text;
            contract.PassWord = txtPassword.Text;
            //向服务器发送登录信息并获取登录结果
            ResMsgContract resMsg = newTcpConnection.SendReceiveObject<LoginContract, ResMsgContract>("ReqLogin", "ResLogin", 5000, contract);
 
            if (resMsg.Message == "登录成功")
            {
                MessageBox.Show("登录成功");
 
            }
            else
            {
                MessageBox.Show("用户名密码错误");
            }
 
        }

原网址:http://www.networkcomms.cn/Article/ViewArticle.aspx?ItemID=27828&PageID=1224

www.networkcomms.cn编辑

NetworkComms V3 模拟登陆

标签:

原文地址:http://www.cnblogs.com/networkcomms/p/4379016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!