Exemplo n.º 1
0
        //接收消息
        private void ReceiveMsg(object clientSocket)
        {
            Socket      mSocket     = (Socket)clientSocket;
            MsgResolver msgResolver = new MsgResolver(dataManager, mSocket, clientMgr);
            string      clientIP    = mSocket.RemoteEndPoint.ToString();

            while (true)
            {
                try
                {
                    byte[] result        = new byte[1024];
                    int    receiveNumber = mSocket.Receive(result);
                    string msg           = Encoding.UTF8.GetString(result, 0, receiveNumber);
                    if (msg.Length <= 0)
                    {
                        throw new Exception("客户端异常中止");
                    }
                    msg = msg.Substring(msg.IndexOf('#'));
                    Console.WriteLine(String.Format("client:{0} command:{1}", clientIP, msg));
                    //将命令交给消息处理类进行处理
                    msgResolver.resolveMsg(msg);
                }
                catch (Exception e)
                {
                    clientMgr.removeClientByIP(clientIP);
                    Console.WriteLine(string.Format("{0} closed ({1})", clientIP, e.Message));
                    break;
                }
            }
        }
Exemplo n.º 2
0
        //返回登录结果
        private void login(string msg)
        {
            //分析参数
            string[] args = msg.Split('#');
            args[0] = resumeSpecStr(args[0]);
            args[1] = resumeSpecStr(args[1]);
            //如果该帐号已在其他地方登录则下线之前的登录
            Clients client = mClientsMgr.getClientByID(args[0]);

            if (client != null && !client.getIP().Equals(mSocket.RemoteEndPoint.ToString()))
            {
                //获取之前登录的连接
                Socket socket = mClientsMgr.getSocketByIP(client.getIP());
                //如果socket为空,说明连接已失效,在客户端管理类中做出清理,并继续登录
                if (socket == null)
                {
                    mClientsMgr.removeClientByID(args[0]);
                }
                else //socket不为空,则下线之前登录
                {
                    //发送下线信息
                    socket.Send(Encoding.UTF8.GetBytes(MyProtocol.head_new_login));

                    Console.WriteLine(socket.RemoteEndPoint.ToString() + " offline");

                    //关闭连接
                    mClientsMgr.closeSocketByIP(socket.RemoteEndPoint.ToString());
                    //从管理类移除客户端
                    mClientsMgr.removeClientByIP(client.getIP());
                }
            }
            //查询对应管理员
            List <AdminInfo> admin = mDataManager.queryAdmins(AdminInfo.type.name, args[0]);

            //比对信息并返回结果
            if (admin.Count <= 0 || !admin[0].getpw().Equals(args[1]))
            {
                //登录失败
                mSocket.Send(Encoding.UTF8.GetBytes(MyProtocol.head_login + MyProtocol.body_false));
                Console.WriteLine(string.Format("{0}({1}) login denied",
                                                mSocket.RemoteEndPoint.ToString(), args[0]));
                return;
            }
            //登录成功
            mSocket.Send(Encoding.UTF8.GetBytes(MyProtocol.head_login + MyProtocol.body_true));
            //添加到管理类
            mClientsMgr.addClient(new Clients(mSocket.RemoteEndPoint.ToString(), args[0], true));
            Console.WriteLine(string.Format("{0}({1}) login success",
                                            mSocket.RemoteEndPoint.ToString(), args[0]));
        }