⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverdlg.cpp

📁 初学VC,编写的一个机房管理系统,功能基本上完善,但代码可读性不强.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "server.h"
#include "serverDlg.h"
#include "EnterDlg.h"
#include "PasswordDlg.h"
#include "Password2Dlg.h"
#include "useractionSet.h"
#include "userSet.h"
#include  "nowSet.h"
#include "ChangePwdDlg.h"
#include <Winsock2.h>
#include <stdio.h>
#include "ViewDlg.h"
#include "ChangeUserPwdDlg.h"
#include "AdminEnterDlg.h"
#include "admactionSet.h"
#include "ViewMeDlg.h"
#define WM_SENDMSGFAIL WM_USER+555
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CWnd *phserver;

//bool tag_of_close_server=false;
bool statue=false;///上机模式的标志,true:为教学上机///false:一般上机
//SOCKET sockSrv; 
bool tagofsend=true;
bool changepwdorfreeze=true;///改密码还是冻结,true为改密码
extern bool tagofhadm;
void jishi()//////////////计时///////////////////////
{	bool tagofupdate=true;
	CTime currenttime=CTime::GetCurrentTime();
   // CString mtime=currenttime.Format("%#H时%#M分%#S秒");
	///////////获得当前时间//////////
	int c_hour=currenttime.GetHour();
	int c_minute=currenttime.GetMinute();
	////////////////从CNOWSET中读取开始时间/////////
	CNowSet usernow;
	usernow.Open();
	while(!usernow.IsEOF())
	{
		//////////////先把NUM读取出来///
		tagofupdate=true;
		if(!usernow.m_statue)////非自由上机
		{
		CUserSet user;
		CString *pxuehao= new CString;
		pxuehao->Format("xuehao='%s'",usernow.m_xuehao);
		user.m_strFilter=*pxuehao;
		delete pxuehao;
		user.Open();
		int num=user.m_num;
		//////////num//////////
		int s_hour=usernow.m_hour;
		int s_minute=usernow.m_minute;
		int usedtime=(c_hour-s_hour)*60+c_minute-s_minute;
		user.Edit();
		user.m_time-=usedtime;
		user.Update();
		//////////////////////////
		CString computer;
		computer=usernow.m_ip;
		computer=computer.Right(2);
		computer.Replace(".","");
		int icomputer=atoi(computer);
		/////////////////////////////
		if(user.m_time<num*5&&user.m_time>0&&tagofsend)
		{
		//MessageBox(NULL,"发送消息提醒其下机","test",MB_OK);
			///////发送消息提醒其下机/////////
			/////这里是给客户端发送信息////////
		SOCKADDR_IN addrSrv;
		SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
		addrSrv.sin_addr.S_un.S_addr=inet_addr(usernow.m_ip);
		addrSrv.sin_family =AF_INET;
		addrSrv.sin_port=htons(6001);
		int ret = 0;
		int error = 0;
		
		char sendmsgbuf[2]="7";
		ret = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
		if(ret == SOCKET_ERROR)
		{
				TRACE("Connect Error: %d \n", (error = WSAGetLastError()));
				if(error == 10061)
				//MessageBox(NULL,"客户端遇到错误,请检查!","错误",MB_ICONERROR | MB_OK);
				//phserver->SendMessage(WM_SENDMSGFAIL,0,MAKELPARAM(0,icomputer));
				return;
		}
		else
		{
			ret = send(sockClient, sendmsgbuf,2, 0);
			if(ret != 2)
			{
			//	MessageBox(NULL,"客户端遇到错误,请检查!","错误",MB_ICONERROR | MB_OK);
			//	phserver->SendMessage(WM_SENDMSGFAIL,0,MAKELPARAM(0,icomputer));
				return;
			}
		}
		}
		else if(user.m_time<=0&&tagofsend)
		{
		//	MessageBox(NULL,"发送消息,锁定客户端","test",MB_OK);
			//////////发送消息,锁定客户端////////////
			SOCKADDR_IN addrSrv;
			SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
			addrSrv.sin_addr.S_un.S_addr=inet_addr(usernow.m_ip);
			addrSrv.sin_family =AF_INET;
			addrSrv.sin_port=htons(6001);
			int ret = 0;
			int error = 0;
			char sendmsgbuf[2]="8";
			//sendmsgbuf[0]='8';
			//sendmsgbuf[1]=char(0);
			ret = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
			if(ret == SOCKET_ERROR)
				{
					TRACE("Connect Error: %d \n", (error = WSAGetLastError()));
					if(error == 10061)
					//MessageBox(NULL,"客户端遇到错误,请检查!","错误",MB_ICONERROR | MB_OK);
					phserver->SendMessage(WM_SENDMSGFAIL,0,MAKELPARAM((WORD)0,(WORD)icomputer));
					return ;
				}
				ret = send(sockClient, sendmsgbuf,2, 0);
				if(ret != 2)
				{
				//	MessageBox(NULL,"客户端遇到错误,请检查","错误!",MB_ICONERROR | MB_OK);
					phserver->SendMessage(WM_SENDMSGFAIL,0,MAKELPARAM((WORD)0,(WORD)icomputer));					return ;
				}
				else
				{
				////////////在now里面对其进行删除//////////////
					
					CString	xuehao=usernow.m_xuehao;
					CString	name=usernow.m_name;
					CString	l_class=usernow.m_class;
					CString ip=usernow.m_ip;
					int statue=usernow.m_statue;
					usernow.Delete();
					tagofupdate=false;
					//now.Requery();
					if(!statue)
					{					
						///////////////useraction添加记录///////////////
						CUseractionSet useraction;
						useraction.Open();
						useraction.AddNew();
						useraction.m_xuehao=xuehao;
						useraction.m_name=name;
						useraction.m_class=l_class;
						useraction.m_action="强迫下机";
						useraction.m_ip=ip;
						CTime currenttime=CTime::GetCurrentTime();
						CString mtime=currenttime.Format("%#Y年%#m月%#d日%#H时%#M分%#S秒");
						useraction.m_date=mtime;
						useraction.Update();
						useraction.Close();
						//////////////////user中的num减一//////////////
						user.Edit();
						user.m_num=user.m_num-1;
						user.Update();
					
					}


				}
		}
		user.Close();
		if(tagofupdate)
		{	usernow.Edit();
			usernow.m_hour=c_hour;
			usernow.m_minute=c_minute;
			usernow.Update();
		}
		}
		usernow.MoveNext();
	}
	usernow.Close();
	tagofsend=true;
}

UINT startserver(void *cs)
{	
	char sendbuf[2];
	SOCKADDR_IN addClient;
	int len=sizeof(SOCKADDR);
	char recvbuf[35];
	SOCKET	sockSrv=(SOCKET)cs;
	 while(1)
	{
			SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addClient,&len);
			int tagrecv=recv(sockConn,recvbuf,35,0);
			if ((tagrecv == 0) || (tagrecv == SOCKET_ERROR))
			{
					AfxMessageBox("连接失败!",MB_OK);
					break;
			}
			if(recvbuf[0]=='1')////为教学上机
			{
				if(statue) 
			/////////////通过验证///////////////
				{
					sprintf(sendbuf,"%d",0);/////////教学上机时间
					////////////对NOW进行记录///////////
					CNowSet nowstudent;
					CString *pip;
					pip= new CString;
					pip->Format("ip='%s'",inet_ntoa(addClient.sin_addr));
					nowstudent.m_strFilter=*pip;
					nowstudent.Open();
					if(nowstudent.GetRecordCount()==0)//////////////客户端的IP不存在,说明他是第一次登录,应记录
					{
					nowstudent.Close();
					nowstudent.Open();
					nowstudent.AddNew();
					nowstudent.m_xuehao="教学上机";
					nowstudent.m_password="password";
					nowstudent.m_name="学生";
					nowstudent.m_ip=inet_ntoa(addClient.sin_addr);
					nowstudent.m_class="班级";
					CTime currenttime=CTime::GetCurrentTime();
					CString mtime=currenttime.Format("%#H时%#M分%#S秒");
					nowstudent.m_starttime=mtime;
					nowstudent.m_statue=1;
					nowstudent.Update();
					nowstudent.Close();
					}
				}
				else
				sprintf(sendbuf,"%c",'b');
				sendbuf[1]='\0';
				send(sockConn,sendbuf,strlen(sendbuf)+1,0);
				shutdown(sockConn, 2);
				closesocket(sockConn);
						
			}
			else if(recvbuf[0]=='2')////////1为教师上机
			{
				CString *msg;
				msg =new CString;
			//////////////以后再加一个字符串转换函数/////////////
				CString computer;
				computer=inet_ntoa(addClient.sin_addr);
				computer=computer.Right(2);
				computer.Replace(".","");
				msg->Format("%s,%s号机上有教师请求上机,是否通过?","管理员",computer);
				int choice=MessageBox(NULL,*msg,"提醒",MB_YESNO|MB_ICONQUESTION);
				delete msg;
				if(choice==IDYES)
				{sprintf(sendbuf,"%d",0);/////////教师上机
				/////////////对NOW进行记录///////////////
					CNowSet nowstudent;
					CString *pip;
					pip= new CString;
					pip->Format("ip='%s'",inet_ntoa(addClient.sin_addr));
					nowstudent.m_strFilter=*pip;
					nowstudent.Open();
					if(nowstudent.GetRecordCount()==0)//////////////客户端的IP不存在,说明他是第一次登录,应记录
					{
					nowstudent.Close();
					nowstudent.Open();
					nowstudent.AddNew();
					nowstudent.m_xuehao="教师上机";
					nowstudent.m_password="password";
					nowstudent.m_name="教师";
					nowstudent.m_ip=inet_ntoa(addClient.sin_addr);
					nowstudent.m_class="班级";
					CTime currenttime=CTime::GetCurrentTime();
					CString mtime=currenttime.Format("%#H时%#M分%#S秒");
					nowstudent.m_starttime=mtime;
					nowstudent.m_statue=2;
					nowstudent.Update();
					nowstudent.Close();
					}
				}
				else
				sprintf(sendbuf,"%d",4);
				sendbuf[1]='\0';
				send(sockConn,sendbuf,strlen(sendbuf)+1,0);
				shutdown(sockConn, 2);
				closesocket(sockConn);
				
			}
			else if(recvbuf[0]=='0')/////////0为一般上机
			{
		////////将recvbuf分解开来////////////////////

				char *ch_xuehao,*ch_password;
				int len1=recvbuf[1];
				ch_xuehao=new char[len1+1];
				for(int i=0;i<len1;i++)
				ch_xuehao[i]=recvbuf[i+2];
				ch_xuehao[i]='\0';
				int len2=recvbuf[len1+2];
				ch_password=new char[len2+1];
				for(i=0;i<len2;i++)
				ch_password[i]=recvbuf[len1+3+i];
				ch_password[i]='\0';
				CString xuehao,password,name,l_class;
				xuehao.Format("%s",ch_xuehao);
				password.Format("%s",ch_password);
				delete ch_xuehao,ch_password;
				//////////////将recvbuf分解开来////////////////////////
				/////验证//////////////////
				CUserSet user;
				CString *pxuehao;
				pxuehao=new CString;
				pxuehao->Format("xuehao='%s'",xuehao);
				user.m_strFilter=*pxuehao;
				delete pxuehao;
				user.Open();
				bool tag_xx=true;
				int tag_of_sj;
				if(!user.GetRecordCount()||user.m_password!=password)///学号密码不正确
				tag_of_sj=1;
				else if(user.m_mark)//账户冻结
					tag_of_sj=2;
				else if(user.m_time<1)///时间不够
				tag_of_sj=3;
				else  //成功
				{tag_of_sj=0;name=user.m_name;l_class=user.m_class;}
				user.Close();
				sprintf(sendbuf,"%d",tag_of_sj);
				sendbuf[1]='\0';
				send(sockConn,sendbuf,strlen(sendbuf)+1,0);
				shutdown(sockConn, 2);
				closesocket(sockConn);
				if(tag_of_sj==0) 
				{	///////////先看是否存在/////////
					jishi();
					CNowSet nowstudent;
					CString *pip;
					pip= new CString;
					pip->Format("ip='%s'",inet_ntoa(addClient.sin_addr));
					nowstudent.m_strFilter=*pip;
					nowstudent.Open();
					if(nowstudent.GetRecordCount()==0)//////////////客户端的IP不存在,说明他是第一次登录,应记录
					{	////////////////////////在now里添加记录///////////////
						
						nowstudent.Close();
						//nowstudent.m_strFilter="";//////////////不知是否必要
						nowstudent.Open();
						nowstudent.AddNew();
						nowstudent.m_xuehao=xuehao;
						nowstudent.m_password=password;
						nowstudent.m_name=name;
						nowstudent.m_ip=inet_ntoa(addClient.sin_addr);
						nowstudent.m_class=l_class;
						CTime currenttime=CTime::GetCurrentTime();
						CString mtime=currenttime.Format("%#H时%#M分%#S秒");
						nowstudent.m_starttime=mtime;
						
						nowstudent.m_hour=currenttime.GetHour();
						nowstudent.m_minute=currenttime.GetMinute();
						nowstudent.Update();
						nowstudent.Close();
						////////////useraction记录////////////////////
						CUseractionSet useraction;
						useraction.Open();
						useraction.AddNew();
						useraction.m_xuehao=xuehao;
						useraction.m_name=name;
						useraction.m_class=l_class;
						useraction.m_ip=inet_ntoa(addClient.sin_addr);
						useraction.m_action="上机";
						currenttime=CTime::GetCurrentTime();
						mtime=currenttime.Format("%#Y年%#m月%#d日%#H时%#M分%#S秒");
						useraction.m_date=mtime;
						useraction.Update();
						useraction.Close();
					/////////////useraction记录////////////////////
					//////////////////////////////////
					///////////计数器加一
						CUserSet user;
						CString *pxuehao= new CString;
						pxuehao->Format("xuehao='%s'",xuehao);
						user.m_strFilter=*pxuehao;
						delete pxuehao;
						user.Open();
						user.Edit();
						user.m_num+=1;
						user.Update();
						user.Close();

					}
				}
			}
			
		   else if(recvbuf[0]=='3')/////下机
		   {
				
				/////把now里的记录删除//////////////////////*/
				tagofsend=false;
				jishi();
				CString xuehao,name,l_class;
				CString *pip= new CString;
				pip->Format("ip='%s'",inet_ntoa(addClient.sin_addr));
				CNowSet now;
				now.m_strFilter=*pip;
				now.Open();
				int statue=0;
				if(now.GetRecordCount())
				{	delete pip;
					xuehao=now.m_xuehao;
					name=now.m_name;
					l_class=now.m_class;
					statue=now.m_statue;
					now.Delete();
					now.Requery();
				}
				now.Close();
				if(!statue)
				{					
				///////////////useraction添加记录///////////////
				CUseractionSet useraction;
				useraction.Open();
				useraction.AddNew();
				useraction.m_xuehao=xuehao;
				useraction.m_name=name;
				useraction.m_class=l_class;
				useraction.m_action="下机";
				useraction.m_ip=inet_ntoa(addClient.sin_addr);
				CTime currenttime=CTime::GetCurrentTime();
				CString mtime=currenttime.Format("%#Y年%#m月%#d日%#H时%#M分%#S秒");
				useraction.m_date=mtime;
				useraction.Update();
				useraction.Close();
				//////////////////user中的num减一//////////////
				CString *pxuehao= new CString;
				pxuehao->Format("xuehao='%s'",xuehao);
				CUserSet user;
				user.m_strFilter=*pxuehao;
				delete pxuehao;
				user.Open();
				user.Edit();
				user.m_num=user.m_num-1;
				user.Update();
				user.Close();
				shutdown(sockConn, 2);
				closesocket(sockConn);
				}
		   }
		   else if(recvbuf[0]=='4')/////更改密码
		   {	
			   /////把学号,密码,新密码分别提取出来;
				char *ch_xuehao,*ch_password,*newch_password;
				int len1=recvbuf[1];
				ch_xuehao=new char[len1+1];
				for(int i=0;i<len1;i++)
				ch_xuehao[i]=recvbuf[i+2];
				ch_xuehao[i]='\0';
				int len2=recvbuf[len1+2];
				ch_password=new char[len2+1];
				for(i=0;i<len2;i++)
				ch_password[i]=recvbuf[len1+3+i];
				ch_password[i]='\0';
				int len3=recvbuf[len1+len2+3];
				newch_password=new char[len3+1];
				for(i=0;i<len3;i++)
				newch_password[i]=recvbuf[len1+len2+4+i];
				newch_password[i]='\0';
				CString xuehao,password,newpassword;
				xuehao.Format("%s",ch_xuehao);
				password.Format("%s",ch_password);
				newpassword.Format("%s",newch_password);
				delete ch_xuehao,ch_password,newch_password;
				///////////验证/////////
				CUserSet user;
				CString *pxuehao;
				pxuehao=new CString;
				pxuehao->Format("xuehao='%s'",xuehao);
				user.m_strFilter=*pxuehao;
				delete pxuehao;
				user.Open();
				if(user.GetRecordCount()&&user.m_password==password)
				{	user.Edit();
					user.m_password=newpassword;
					user.Update();
					sprintf(sendbuf,"%d",5);
				}
				else
					sprintf(sendbuf,"%d",6);
				user.Close();
				sendbuf[1]='\0';
				send(sockConn,sendbuf,strlen(sendbuf)+1,0);
				shutdown(sockConn, 2);
				closesocket(sockConn);
		   }
		   else if(recvbuf[0]=='5')/////////////客户端发送查看上机情况的请求///
		   {	tagofsend=false;
				jishi();
				char *ch_xuehao;
				int len1=recvbuf[1];
				ch_xuehao=new char[len1+1];
				for(int i=0;i<len1;i++)
				ch_xuehao[i]=recvbuf[i+2];
				ch_xuehao[i]='\0';
				CString xuehao;
				xuehao.Format("%s",ch_xuehao);
				delete ch_xuehao;
				CUserSet user;
				CString *pxuehao;
				pxuehao=new CString;
				pxuehao->Format("xuehao='%s'",xuehao);
				user.m_strFilter=*pxuehao;
				delete pxuehao;
				user.Open();
				int time=user.m_time;
				user.Close();
				char *sendbuf2= new char[5];
				sprintf(sendbuf2,"%d",time);
				int leni=strlen(sendbuf2);
				sendbuf2[i]='\0';
				send(sockConn,sendbuf2,strlen(sendbuf2)+1,0);
				delete sendbuf2;
				shutdown(sockConn, 2);
				closesocket(sockConn);
				
		   }

		   else
		   {	shutdown(sockConn, 2);
				closesocket(sockConn);
		   }
  }
    WSACleanup();
	ExitThread(0);
	return 1;

}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -