Exemple #1
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="clientSocket"></param>
        private static void ReceiveMessage(object clientSocket)
        {
            Program p = new Program();
            Socket  myClientSocket = (Socket)clientSocket;

            byte[]      mmbytes = SocketHelper.ReceiveVarData(myClientSocket);
            ModelMember member  = null;

            try {
                member = (ModelMember)db.BytetoClass(mmbytes, "VCDS");
                string ipaddress = IPAddress.Parse(((IPEndPoint)myClientSocket.RemoteEndPoint).Address.ToString()).ToString();

                switch (member.strCommand)
                {
                case "login":
                    EchoHelper.Echo("接收客户端(" + ipaddress + ")登录名:" + member.netname + "密码:" + member.netpass, "接收信息", EchoHelper.EchoType.普通信息);

                    //用户cookies服务器端验证。
                    if (p.ValidateUser(ref member))
                    {
                        member.strCommand    = "login";
                        member.bLoginSuccess = true;
                    }
                    else
                    {
                        member.strCommand    = "exit";
                        member.bLoginSuccess = false;
                    }

                    ClientItem tmp = null;
                    if ((tmp = p.CheckUserExist(member)) != null && member.bLoginSuccess && member.ipaddress != tmp.clientMember.ipaddress)
                    {
                        //此时用户名和密码同时相同,开始踢客户端.
                        tmp.clientMember.strCommand = "exit";
                        tmp.clientMember.strMessage = "您的账户在另一地点登录:" + ipaddress + ",您被迫下线!建议您修改密码!";
                        byte[] senddata = db.ClasstoByte(tmp.clientMember, "VCDS");
                        if (!tmp.clientSocket.Connected)
                        {
                            tmp.clientSocket.Connect(tmp.clientSocket.RemoteEndPoint);
                        }
                        member = tmp.clientMember.CopyTo();
                        //删除前一客户端
                        clientList.Remove(tmp);
                        EchoHelper.Echo(member.netname + "(" + member.ipaddress + "),被踢下线了!", "用户验证", EchoHelper.EchoType.错误信息);
                        SocketHelper.SendVarData(tmp.clientSocket, senddata);
                        tmp.clientSocket.Shutdown(SocketShutdown.Both);
                        tmp.clientSocket.Close();
                    }

                    //记录当前客户端.
                    member.ipaddress = ipaddress;
                    ClientItem cur = new ClientItem()
                    {
                        clientMember = member, clientSocket = myClientSocket
                    };

                    byte[] curdata = db.ClasstoByte(member, "VCDS");
                    SocketHelper.SendVarData(myClientSocket, curdata);
                    if (member.bLoginSuccess)
                    {
                        clientList.Add(cur);
                    }
                    break;

                case "closeform":
                    member.ipaddress = ipaddress;
                    if ((tmp = p.GetUser(member)) != null)
                    {
                        clientList.Remove(tmp);
                        EchoHelper.Echo(member.netname + "(" + member.ipaddress + "),下线了.", "用户验证", EchoHelper.EchoType.任务信息);
                        //member.strCommand = "quit";
                        //byte[] quitdatas = db.ClasstoByte(member, "VCDS");
                        //SocketHelper.SendVarData(tmp.clientSocket, quitdatas);
                    }
                    break;

                default:
                    break;
                }
            } catch (Exception ex) {
                EchoHelper.Echo("quit" + ex.Message, "exception", EchoHelper.EchoType.异常信息);
            }
        }