Demo2 模拟简单登陆 服务器端代码

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //服务器开始监听客户端的请求           
            IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));   
            TCPConnection.StartListening(thePoint, 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("ResLogin", contract);

            }
            catch (Exception ex)
            {
               
            }
        }
       //http://shop115882994.taobao.com
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            NetworkComms.Shutdown();
            this.Dispose();
            this.Close();
        }
    }
 www.networkcomms.cn编辑
http://www.cnblogs.com/networkcomms

 

你可能感兴趣的:(Demo2 模拟简单登陆 服务器端代码)