/// <summary>
        /// 学生手机客户端计时请求
        /// </summary>
        /// <param name="studentClient">学生手机客户端</param>
        /// <param name="stuId">学号</param>
        /// <param name="keepTime">持续计时时间</param>
        private void timeReq(attendanceServerSocket studentClient, string stuId, int keepTime)
        {
            JObject timeRsp = new JObject();

            timeRsp.Add("type", "timeRsp");
            int score = (int)(((float)keepTime / (float)attendanceServerInfo.getAttendanceServerInfo().getStartServerInterval()) * (float)100);

            if (score >= 100)
            {
                score = 100;
            }
            mysqlImp.getMysql().update("update timeattendance set score=" + score + ",keepTime = " + keepTime + " where stuId='" + stuId + "'");
            DataTable table = mysqlImp.getMysql().showTable("select keepTime,score from timeattendance where stuId='" + stuId + "'");

            timeRsp.Add("keepTime", aesImp.getAesImp().encrypt(
                            table.Rows[0]["keepTime"].ToString(),
                            attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
            timeRsp.Add("score", aesImp.getAesImp().encrypt(
                            table.Rows[0]["score"].ToString(),
                            attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
            mysqlImp.getMysql().update("update timeattendance set allcore = score + ecore where stuId='" + stuId + "'");
            mysqlImp.getMysql().update("update timeattendance set allcore = 100 where allcore > 100 and stuId='" + stuId + "'");
            mysqlImp.getMysql().update("update timeattendance set allcore = 0 where allcore < 0 and stuId='" + stuId + "'");
            studentClient.sendMsg(timeRsp.ToString());
        }
        /// <summary>
        /// 连接回复
        /// </summary>
        /// <param name="studentClient">学生手机客户端对象</param>
        public void connRsp(attendanceServerSocket studentClient)
        {
            JObject connRsp = new JObject();

            connRsp.Add("type", "connRsp");
            connRsp.Add("randomKey", attendanceServerInfo.getAttendanceServerInfo().getRandomKey());
            connRsp.Add("attendanceType", loadStudentListInfo.getLoadStudent().getAttendanceType().ToString());
            attendanceInfo.getAttendance().getStartCheck().showServerReceive("发送数据给客户端:" + connRsp.ToString());
            studentClient.sendMsg(connRsp.ToString());
        }
        /// <summary>
        /// 学生手机客户端消息管理
        /// </summary>
        /// <param name="studentClient">学生手机客户端对象</param>
        /// <param name="messageStr">客户端发送给服务端的消息</param>
        public void messageManager(attendanceServerSocket studentClient, string messageStr)
        {
            JObject reqMessage = JObject.Parse(messageStr);

            switch (reqMessage["type"].ToString())
            {
            case "signReq":
            {
                string stuId = aesImp.getAesImp().decrypt(
                    reqMessage["stuId"].ToString(),
                    attendanceServerInfo.getAttendanceServerInfo().getRandomKey());
                string stuTele = aesImp.getAesImp().decrypt(
                    reqMessage["stuTele"].ToString(),
                    attendanceServerInfo.getAttendanceServerInfo().getRandomKey());
                string stuMac = aesImp.getAesImp().decrypt(
                    reqMessage["stuMac"].ToString(),
                    attendanceServerInfo.getAttendanceServerInfo().getRandomKey());
                signStudent(studentClient, stuId, stuTele, stuMac);
            }
            break;

            case "timeReq":
            {
                // keepTime save and calculate classCore
                string stuId = aesImp.getAesImp().decrypt(
                    reqMessage["stuId"].ToString(),
                    attendanceServerInfo.getAttendanceServerInfo().getRandomKey());
                int keepTime = Convert.ToInt32(aesImp.getAesImp().decrypt(
                                                   reqMessage["keepTime"].ToString(),
                                                   attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                timeReq(studentClient, stuId, keepTime);
            }
            break;

            default: break;
            }
        }
Пример #4
0
        /// <summary>
        /// 服务端线程
        /// </summary>
        private void attendanceServerThread()
        {
            setCurrentTime();
            saveServerKey();
            Socket serverSocket = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);

            serverSocket.Bind(new IPEndPoint(
                                  IPAddress.Parse(serverInfo.getServerInfo().getServerIP()),
                                  serverInfo.getServerInfo().getServerPort()
                                  ));
            formInfo.getFormInfo().getForm_attendance().setCurrentInterval();
            try
            {
                serverSocket.Listen(3000);          // 最大监听客户端数量
                formInfo.getFormInfo().getForm_attendance().startCheck();
                attendanceInfo.getAttendance().getStartCheck().showServerReceive("服务端启动成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("服务端启动失败:" + ex.ToString());
            }
            if (serverSocket.IsBound)
            {
                while (true)
                {
                    Socket client = serverSocket.Accept();
                    attendanceInfo.getAttendance().getStartCheck().showServerReceive("发现客户端 :" + client.RemoteEndPoint);
                    attendanceServerSocket attendanceSocket = new attendanceServerSocket(client);
                    new Thread(new ThreadStart(attendanceSocket.receiveMsg)).Start();
                    attendanceServerManager.getManager().addStudentClient(attendanceSocket);
                }
            }
        }
 /// <summary>
 /// 查出学生手机客户端对象
 /// </summary>
 /// <param name="studentClient">学生手机客户端对象</param>
 public void removeStudentClient(attendanceServerSocket studentClient)
 {
     studentClientList.Remove(studentClient);
 }
 /// <summary>
 /// 更新学生手机客户端对象
 /// </summary>
 /// <param name="studentClient">学生手机客户端对象</param>
 /// <param name="stuId">学号</param>
 public void updateStudentClient(attendanceServerSocket studentClient, string stuId)
 {
     studentClientList[studentClient] = stuId;
 }
 /// <summary>
 /// 添加学生手机客户端对象
 /// </summary>
 /// <param name="studentClient">学生手机客户端对象</param>
 public void addStudentClient(attendanceServerSocket studentClient)
 {
     studentClientList.Add(studentClient, "");
     connRsp(studentClient);
 }
        /// <summary>
        /// 学生签到
        /// </summary>
        /// <param name="studentClient">学生手机客户端对象</param>
        /// <param name="stuId">学生学号</param>
        /// <param name="stuTele">学生手机号码</param>
        /// <param name="stuMac">学生手机MAC地址</param>
        private void signStudent(attendanceServerSocket studentClient, string stuId, string stuTele, string stuMac)
        {
            JObject signRsp = new JObject();

            signRsp.Add("type", "signRsp");
            if (checkStudentExist(stuId))
            {
                if (checkStudentSign(stuId))
                {
                    // 检测该学号已经签到过了
                    if (loadStudentListInfo.getLoadStudent().getAttendanceType())
                    {
                        // 检测考勤模式处于计时模式
                        if (checkStudentTeleInfo(stuId, stuTele, stuMac))
                        {
                            // 回复计时
                            returnTime(stuId, signRsp);
                            signRsp.Add("returnType", "2");
                        }
                        else
                        {
                            // 回复计时失败,原因:该学号的手机号码和手机MAC信息与签到时的信息不一致
                            signRsp.Add("result", aesImp.getAesImp().encrypt(
                                            "该学号的手机号码和手机MAC信息与签到时的信息不一致",
                                            attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                            signRsp.Add("returnType", "0");
                        }
                    }
                    else
                    {
                        // 检测考勤模式处于签到模式,签到失败:该学号已经签到过了
                        signRsp.Add("result", aesImp.getAesImp().encrypt(
                                        "该学号已经签到过了",
                                        attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                        signRsp.Add("returnType", "0");
                    }
                }
                else
                {
                    // 检测该学号没有签到
                    if (checkStudentTele(stuTele))
                    {
                        // 检测该手机号码已经被使用
                        signRsp.Add("result", aesImp.getAesImp().encrypt(
                                        "该手机号码已经被使用",
                                        attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                        signRsp.Add("returnType", "0");
                    }
                    else
                    {
                        // 该手机号码没被使用
                        if (checkStudentMac(stuMac))
                        {
                            // 检测该手机设备已经被使用
                            signRsp.Add("result", aesImp.getAesImp().encrypt(
                                            "该手机设备已经被使用",
                                            attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                            signRsp.Add("returnType", "0");
                        }
                        else
                        {
                            // 检测该手机设备没有被使用
                            // 学生签到
                            sign(stuId, stuTele, stuMac);
                            if (loadStudentListInfo.getLoadStudent().getAttendanceType())
                            {
                                signRsp.Add("result", aesImp.getAesImp().encrypt(
                                                "签到成功,请认真上课",
                                                attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                            }
                            else
                            {
                                signRsp.Add("result", aesImp.getAesImp().encrypt(
                                                "签到成功,计时过程请关闭屏幕并认真上课",
                                                attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                            }
                            signRsp.Add("interval", aesImp.getAesImp().encrypt(
                                            attendanceServerInfo.getAttendanceServerInfo().getStartServerInterval().ToString()
                                            , attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
                            signRsp.Add("returnType", "1");
                            updateStudentClient(studentClient, stuId);
                            if (!loadStudentListInfo.getLoadStudent().getAttendanceType())
                            {
                                mysqlImp.getMysql().update("update signattendance set allcore = score + ecore where stuId='" + stuId + "'");
                                mysqlImp.getMysql().update("update signattendance set allcore = 100 where allcore > 100 and stuId='" + stuId + "'");
                                mysqlImp.getMysql().update("update signattendance set allcore = 0 where allcore < 0 and stuId='" + stuId + "'");
                            }
                        }
                    }
                }
            }
            else
            {
                signRsp.Add("result", aesImp.getAesImp().encrypt(
                                "学号不存在,请重新输入学号",
                                attendanceServerInfo.getAttendanceServerInfo().getRandomKey()));
            }
            studentClient.sendMsg(signRsp.ToString());
            attendanceInfo.getAttendance().getStartCheck().showServerReceive(signRsp.ToString());
        }