[NET] Socket Server (二)

  • 1234
  • 0

摘要:[NET] Socket Server (二)

服務器:MSGServer 是個TCP Socket Server 負責連接及轉發資料

 

使用者登入範例

透過MSGAPP連接服務器'接受服務器的MSGClient登入的帳密檢查

在這裡加入帳號、密碼、APPName的验证规则,可以是本地文字档、资料库等

 protected MSGAPP.Client clientApp = null;

        protected void appStart()
        {
            if (isStart)
            {
                isStart = false;
                if (clientApp != null)
                    clientApp.Stop();
                clientApp = null;
            }
            else
            {
                isStart = true;
                StartTime = DateTime.Now;
                clientApp = new MSGAPP.Client(AppName, AppName, Settings.ServerIP, MSGGolbalInfo.Settings.DicDefPort["APP"]);
                clientApp.ClientStateEvent += new MSGGolbalInfo.ClientStateEventArgs(clientApp_ClientStateEvent);
                clientApp.DataReceivedEvent += new EventHandler(clientApp_DataReceivedEvent);
                clientApp.CommandReceivedEvent += new EventHandler(clientApp_CommandReceivedEvent);
                clientApp.ResultsReceivedEvent += new EventHandler(clientApp_ResultsReceivedEvent);
                clientApp.CommonReceivedEvent += new EventHandler(clientApp_CommonReceivedEvent);
                clientApp.SettingEvent += new EventHandler(clientApp_SettingEvent);
                clientApp.Start();
                setAppInfo();
            }
            Start(!isStart);
        }
 protected override void setAppInfo()



        {
            if (clientApp == null) return;

            clientApp.Info = new MSGGolbalInfo.UserInfo();

            clientApp.Info.Add(AppName, new MSGGolbalInfo.UserInfoItem(AppName));
            clientApp.Info[AppName].Add(Keys.Client.LocalMode, 500); //登入检查的标签,Level 值

            //跟服務器登記我是LoginServer的執行者
            clientApp.Info.Add(Keys.Execution, new MSGGolbalInfo.UserInfoItem(Keys.Execution));
            clientApp.Info[Keys.Execution].Add(Keys.Client.LoginServer, 500); //登入检查的标签,Level 值
            clientApp.Info[Keys.Execution].Add(Keys.JSON.LoginServer, 500); //登入检查的标签,Level 值

            //跟服務器登記我要監看user的登出
            clientApp.Info.Add(Keys.Watch, new MSGGolbalInfo.UserInfoItem(Keys.Watch));
            clientApp.Info[Keys.Watch].Add(Keys.Client.DEL, true);
            clientApp.Info[Keys.Watch].Add(Keys.JSON.DEL, true); 
          
            if (clientApp.isStart)
                clientApp.setInfo();
        }

   protected override void clientApp_CommandReceivedEvent(object sender, EventArgs e)
        #region >><<
        {
            if (e is MSGGolbalInfo.ULogin)
            {
                ULogin args = (ULogin)e;
                args.IsResult = true;
                args.Success = true;

                Account acc = args.Acc;
                if (acc != null && !string.IsNullOrWhiteSpace(acc.UserName))
                {
                    User user1 = lstUser.Find(r => r.UserName.ToLower() == acc.UserName.ToLower() && r.Password == acc.Password);
                    if (user1 == null)
                    {
                        //不通過
                        print("[{0}] Login fail {1} - {2} - {3}", DateTime.Now.ToString("mm:dd:ss"), acc.UserName, acc.Password, args.APPName);
                    }
                    else
                    {
                        args.isLoginSuccess = true;
                        print("[{0}] Login {1} - {2} - {3}", DateTime.Now.ToString("mm:dd:ss"), acc.UserName, acc.Password, args.APPName);
                    }
                }
                else
                {
                    //不通過
                    print("[{0}] Login fail {1} - {2} - {3}", DateTime.Now.ToString("mm:dd:ss"), acc.UserName, acc.Password, args.APPName);
                }

                clientApp.SendData(TCPGolbalInfo.FlagEnum.Cmd, args);
            }
        }
        #endregion

 

 

程序利用MSGClient作连线,连接到服务器后,服务器寻找登入检查的Client端、如果没有会利用本地文字档资料作检查

 

protected MSGClient.Client clientUser = null;
      
        protected void userStart()
        {
            if (isStart)
            {
                isStart = false;
                if (clientUser != null)
                    clientUser.Stop();
                clientUser = null;
            }
            else
            {
                isStart = true;
                StartTime = DateTime.Now;
                clientUser = new MSGClient.Client(AppName, UserName, Password, Settings.ServerIP, MSGGolbalInfo.Settings.DicDefPort["Client"]);
                clientUser.DataReceivedEvent += new EventHandler(clientUser_DataReceivedEvent);
                clientUser.CommandReceivedEvent += new EventHandler(clientUser_CommandReceivedEvent);
                clientUser.ClientStateEvent += new MSGGolbalInfo.ClientStateEventArgs(clientUser_ClientStateEvent);
                clientUser.SettingEvent += new EventHandler(clientUser_SettingEvent);
                clientUser.Start();
                setUserInfo();
            }
            
        }

protected override void clientUser_ClientStateEvent(object sender, TCPGolbalInfo.FlagEnum flag)
        {
            if (flag == TCPGolbalInfo.FlagEnum.LoginOK)
            {
                MessageBox.Show("登入成功");
            }
            else if (flag == TCPGolbalInfo.FlagEnum.LoginFail || flag == TCPGolbalInfo.FlagEnum.Logout)
            {
                MessageBox.Show("登入失敗");
                userStart();
            }
            else
            {
                Console.WriteLine(flag.ToString());
            }
        }

 

 

 

所以利用MSGAPP.DLL跟MSGClient.DLL程序即可拥有网路连线功能、两者接口类似、可以互换。

 

 

===================

搞了C#

現在來學學維修筆電