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

📄 serverdlg.cpp

📁 用Visual C++实现的网络聊天室
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	else
	{
		AfxMessageBox("信息接受错误!");
	}
		delete pBuf;

}



	void CServerDlg::SendMsg(CString& strText,CClientSocket* pSocket)
	{
		int nLen;
	    int nSent;
		nLen=strText.GetLength();
		nSent=pSocket->Send(LPCTSTR(strText),nLen);
		if(nSent==SOCKET_ERROR)
		{
			AfxMessageBox("消息发送错误!");
		}
	//	m_msgEdit.Empty();
		UpdateData(FALSE);

	}


	void CServerDlg::SendMsgAll(CString& strText)
	{
		POSITION pos;
		for(int i=0;i<userNumber;i++)
		{
			
			pos=m_connectionList.FindIndex(i);
			CClientSocket* pSocket=(CClientSocket*)m_connectionList.GetPrev(pos);
			SendMsg(strText,pSocket);
			
		}
		m_editMsg.Empty();

	}


	    void CServerDlg::DisplayMsg(LPCTSTR lpszMessage)
	{
		m_listMsg.AddString(lpszMessage);
	}



void CServerDlg::OnTimer(UINT nIDEvent) 
{

   	CTime a=CTime::GetCurrentTime();
    CString b=a.Format("%H:%M:%S");
	m_t.SetWindowText(b);
	CDialog::OnTimer(nIDEvent);
}


CString CServerDlg::AddTimeMsg(CString& text)
{
	CTime a=CTime::GetCurrentTime();
    CString b=a.Format("(%H:%M:%S)");
    b=b+text;
	return b;
}



CString CServerDlg::BuildSystemMsg(/*CClientSocket* pSocket,CMsg* preMsg*/)
{

	CString a("a");
	return a;
	
}



bool CServerDlg::LoadUserInfor()
{
	CString pFileName("user.dat");
	CString inforStream;
	CString name;


	if( m_userInforFile.Open( pFileName, CFile::modeRead | CFile::typeText ) )
	{
		m_userInforFile.SeekToBegin();

		
		while(m_userInforFile.ReadString(inforStream))
		{

			if(BuildUser(inforStream))
			{
				User *pUser=new User();
				pUser->m_callName=m_pUser.m_callName;
				pUser->m_password=m_pUser.m_password;
				pUser->m_loadName=m_pUser.m_loadName;
				this->m_userInforList.AddTail(pUser);
			}
			else
			{
				AfxMessageBox("错误信息:"+inforStream);
			}
		
		}
		m_userInforFile.Close();
		
		return true;
	}
	else
	{
		return false;
	}

}



void CServerDlg::DeleteSocket(CClientSocket* pSocket)
	{
		pSocket->Close();
		POSITION pos,temp;
		for(pos=m_connectionList.GetHeadPosition();pos!=NULL;)
		{
			temp=pos;
			CClientSocket* pSock=(CClientSocket*)m_connectionList.GetNext(pos);
			if(pSock==pSocket)
			{
				m_connectionList.RemoveAt(temp);
				userNumber--;
				break;
			}

		}
		delete pSocket;

	}

HBRUSH CServerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	if (pWnd == this) 
	{ 
      return m_brBk; 
	} 
	
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CServerDlg::OnButtonLend() 
{
		while(true)
	{
		m_dlg=new CLoadDlg();
		m_dlg->m_portNumber=8000;
		m_dlg->m_editName="Administrator";
		m_dlg->m_editPassword="1988822";
		UpdateData(TRUE);
		if(m_dlg->DoModal()==IDOK)
		{
			CString errorStr=_T("");
			if(m_dlg->m_portNumber<8000)
			{
				errorStr="输入端口号不小于8000\n";
			}
			else if(m_dlg->m_portNumber>9000)
			{
				errorStr="输入端口号不大于9000\n";
			}
			if(m_dlg->m_editName!="Administrator")
			{
				errorStr=errorStr+"用户名错误!\n";
			}
			if(m_dlg->m_editPassword!="1988822")
			{
				errorStr=errorStr+"密码错误\n";
			}

			if(errorStr!="")
			{
				if(AfxMessageBox(errorStr,MB_YESNO)==IDNO)
				{
					break;
				}
			}
			else
			{


                // port=dlg.m_portNumber;
				 GetDlgItem(IDC_BUTTON_SERVICE1)->EnableWindow(TRUE);
                 GetDlgItem(IDC_BUTTON_LEND)->EnableWindow(FALSE);
				 break;

			}
		}
	}
	
}

/*void CServerDlg::OnButtonService1() 
{
	GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
    GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
    GetDlgItem(IDC_LIST_MSG)->EnableWindow(TRUE);
    GetDlgItem(IDC_LIST_USER)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT_MSG)->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_SERVICE)->EnableWindow(FALSE);

   	 if(m_pSocket->Create(m_dlg->m_portNumber))
	 {
	      if(!m_pSocket->Listen())
		  {
		       AfxMessageBox("侦听失败!");
		 
		  }
	 }
	
}*/

void CServerDlg::OnButtonSend() 
{
	UpdateData(TRUE);


   CString b=AddTimeMsg(m_editMsg);

	m_listMsg.AddString(b);
	SendMsgAll(b);
	m_editMsg=_T("");
	UpdateData(FALSE);	
}

void CServerDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

CString CServerDlg::BuildUserListMsg()
{
	CString a;
	a="USERLIST";
	for(int i=0;i<m_listUser.GetCount();i++)
	{
		CString b;
		m_listUser.GetText(i,b);
		a=a+"_"+b;

	}
	return a;

}

void CServerDlg::OnSelchangeListUser() 
{

}


bool CServerDlg::BuildUser(CString& inforStream)
{
	int begin=0;
	int end=inforStream.Find("_",begin);
	m_pUser.m_loadName=inforStream.Mid(begin,end-begin);

	begin=end+1;
	end=inforStream.Find("_",begin);

	m_pUser.m_password=inforStream.Mid(begin,end-begin);

	begin=end+1;
	m_pUser.m_callName=inforStream.Mid(begin);
	if((!m_pUser.m_callName.IsEmpty())&&
		(!m_pUser.m_loadName.IsEmpty())&&
		(!m_pUser.m_password.IsEmpty()))
	{
		return true;
	}
	else
	{
	    return false;
	}

}


void CServerDlg::OnButtonNew() 
{

	if(m_pNewUserDlg->DoModal()==IDOK)
	{
		
			User *p=new User();
			p->m_loadName=m_pNewUserDlg->m_user;
			p->m_callName=m_pNewUserDlg->m_name;
			p->m_password=m_pNewUserDlg->m_password;
		

			if(!IsExist(p))
			{
			
			    m_pNewUserDlg->m_user.Empty();
		     	m_pNewUserDlg->m_name.Empty();
			    m_pNewUserDlg->m_password.Empty();
				m_pNewUserDlg->m_passwordAgain.Empty();
			//	m_pNewUserDlg->UpdateData(TRUE);
			//	UpdateData(TRUE);
				this->m_userInforList.AddTail(p);
				AddUserItem(p);
			}
			else
			{
				AfxMessageBox("此用户已经存在!");
			}
	
	}
	
	
}


bool CServerDlg::IsExist(User* pUser)
{
	POSITION pos;
	for(int i=0;i<m_userInforList.GetCount();i++)
	{
		pos=m_userInforList.FindIndex(i);
		User* ppUser=(User*)m_userInforList.GetPrev(pos);
		if( ppUser->m_callName==pUser->m_callName)
		{
			return true;
		}
		else if(ppUser->m_loadName==pUser->m_loadName)
		{
			return true;
		}
			
	}
	return false;

}


bool CServerDlg::AddUserItem(User* pUser)

{
	CString pFileName("user.dat");
	  if( m_userInforFile.Open( pFileName, CFile::modeWrite | CFile::typeText ) )
	{
		m_userInforFile.SeekToEnd();
		CString item=pUser->m_loadName+"_"+pUser->m_password+"_"+pUser->m_callName+"\n";
		m_userInforFile.WriteString(item);
		AfxMessageBox("成功");
		m_userInforFile.Close();
		return true;
	}
	  else
	  {
		  return false;
	  }


}


CString CServerDlg::GetUserItem(CString& name,CString& password)
{
	POSITION pos;
	CString a=_T("");
	for(int i=0;i<m_userInforList.GetCount();i++)
	{
		pos=m_userInforList.FindIndex(i);
		User* ppUser=(User*)m_userInforList.GetPrev(pos);
		if(ppUser->m_loadName==name&&ppUser->m_password==password)
		{
             a="SUCCESS_"+ppUser->m_callName;
		//	 AfxMessageBox("GetUserItem"+a);
			 break;
		}
			
	}
    return a;
}


	CString CServerDlg::FindCallName(CString& loadName)
	{
		POSITION pos;
	    for(int i=0;i<m_userInforList.GetCount();i++)
		{
		    pos=m_userInforList.FindIndex(i);
		    User* ppUser=(User*)m_userInforList.GetPrev(pos);
		    if( ppUser->m_loadName==loadName)
			{
			
			    return ppUser->m_callName;
			}
			
		}
	    return "";
	}






void CServerDlg::DeleteAllConnect()
{
		POSITION pos;
		for(int i=0;i<userNumber;i++)
	{
			
		pos=m_connectionList.FindIndex(i);
		CClientSocket* pSocket=(CClientSocket*)m_connectionList.GetPrev(pos);
		pSocket->Close();
			
	}

	m_connectionList.RemoveAll();
}

⌨️ 快捷键说明

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