www.pudn.com > chat-dialog.rar > Sever1_2Dlg.cpp


// Sever1_2Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Sever1_2.h" 
#include "Sever1_2Dlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSever1_2Dlg dialog 
 
CSever1_2Dlg::CSever1_2Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CSever1_2Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CSever1_2Dlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	m_bConnect=FALSE; 
	m_pStringList=new CStringList; 
	for (int ix=0;ix<20;ix++) 
	{ 
		m_pOnLineTime[ix]=NULL; 
	} 
	 
 
} 
 
void CSever1_2Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CSever1_2Dlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
 
} 
 
BEGIN_MESSAGE_MAP(CSever1_2Dlg, CDialog) 
	//{{AFX_MSG_MAP(CSever1_2Dlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart) 
	ON_BN_CLICKED(IDC_SAVEMSG, OnSavemsg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSever1_2Dlg message handlers 
 
BOOL CSever1_2Dlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	CBitmap bitmap; 
	bitmap.LoadBitmap(IDB_CLIENTQQ); 
	m_imageList.Create(20,20,ILC_COLOR,0,5); 
	m_imageList.Add(&bitmap,RGB(0,0,0)); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CSever1_2Dlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CSever1_2Dlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CSever1_2Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
 
 
 
void CSever1_2Dlg::OnButtonStart()  
{ 
	if (m_bConnect==TRUE) 
	{ 
		AfxMessageBox("You have run it!"); 
		return; 
	} 
	CDlgIpPort dlg; 
	dlg.DoModal(); 
	if (dlg.m_bStart==false) 
		return; 
	m_strIP=dlg.m_strIP; 
	m_nPort=dlg.m_nPort; 
	m_socketListen.m_ntotleNum=dlg.m_ntotalNum; 
	// TODO: Add your control notification handler code here 
	 
	if (m_socketListen.Create(m_nPort,SOCK_STREAM,m_strIP)) 
	{ 
		m_socketListen.Listen(2); 
		m_socketListen.m_pParentWnd=this; 
		AfxMessageBox("OK!You have initiate a Sever!"); 
		m_bConnect=TRUE; 
	} 
	else  
		AfxMessageBox("Can not start connect!"); 
} 
 
LRESULT CSever1_2Dlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	switch(message) { 
	case WM_UPDLGDATA: 
		DisDlgText((char*)wParam,(int)lParam); 
		break; 
	case WM_CLOSESOCK: 
		CloseSocket((SOCKET)wParam); 
		break; 
	case WM_SETNEWCLIENT: 
		SetNewSock((SOCKET)wParam); 
		break; 
	case WM_SETNEWSOCKET: 
		SetNewClient((char*)wParam,(int)lParam); 
		break; 
	case WM_CLOSECLIENT: 
		CloseClient((char*)wParam,(int)lParam); 
		break; 
	default: 
		break; 
	} 
	return CDialog::DefWindowProc(message, wParam, lParam); 
} 
 
void CSever1_2Dlg::DisDlgText(char* chDlgText, int nDlgLen) 
{ 
 
//	char* chDlgText=new char; 
//	chDlgText=(char*)wParam; 
//	int nDlgLen=(int)lParam; 
	CString sDlg(chDlgText); 
  
//	 CString sPre; 
//	 GetDlgItemText(IDC_EDIT_REV,sPre); 
	 
//	 GetDlgItem(IDC_EDIT_REV)->SetWindowText(sPre); 
	 SendToAllClient(sDlg,nDlgLen); 
	 strReceive+=sDlg; 
	 strReceiveLen+=nDlgLen; 
   	CSocketMsg* sockMsg=new CSocketMsg; 
	 sockMsg->FormatSocketMsg(sDlg); 
//	OnListChange(sockMsg->ClientID); 
	 CString strform=FormatStr(sockMsg); 
	 m_sFormatDlg+=strform; 
	 SetDlgItemText(IDC_EDIT_REV,m_sFormatDlg); 
	delete sockMsg; 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_REV); 
	int nlen=pEdit->GetLineCount(); 
	pEdit->LineScroll(nlen-1); 
} 
void CSever1_2Dlg::DisDlgText(CString sDlg, int nDlgLen) 
{ 
 
//	char* chDlgText=new char; 
//	chDlgText=(char*)wParam; 
//	int nDlgLen=(int)lParam; 
//	CString sDlg(chDlgText); 
  
//	 CString sPre; 
//	 GetDlgItemText(IDC_EDIT_REV,sPre); 
	 
//	 GetDlgItem(IDC_EDIT_REV)->SetWindowText(sPre); 
	 SendToAllClient(sDlg,nDlgLen); 
	 strReceive+=sDlg; 
	 strReceiveLen+=nDlgLen; 
   	CSocketMsg* sockMsg=new CSocketMsg; 
	 sockMsg->FormatSocketMsg(sDlg); 
//	OnListChange(sockMsg->ClientID); 
	 CString strform=FormatStr(sockMsg); 
	 m_sFormatDlg+=strform; 
	 SetDlgItemText(IDC_EDIT_REV,m_sFormatDlg); 
	delete sockMsg; 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_REV); 
	int nlen=pEdit->GetLineCount(); 
	pEdit->LineScroll(nlen-1); 
} 
 
BOOL CSever1_2Dlg::DestroyWindow()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	m_socketListen.Close(); 
	CAboutDlg dlg; 
	dlg.DoModal(); 
	return CDialog::DestroyWindow(); 
} 
 
void CSever1_2Dlg::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	CSocketMsg* sockMsg=new CSocketMsg; 
	sockMsg->ClientID="Sever"; 
	sockMsg->Time=GetCurTime(); 
	sockMsg->SendMsg="Sever is shut down!"; 
	CString str=sockMsg->FormatMsg(); 
	SendToAllClient(str,str.GetLength()); 
	delete sockMsg;		 
	delete m_pStringList; 
	//delete m_pOnLineTime; 
	CDialog::OnCancel(); 
} 
void CSever1_2Dlg::SendToAllClient(CString strSend,int nLen) 
{ 
	CSocket* socketShutdown; 
	for (int ix=0;ixm_pParentWnd=this; 
//		m_socketListen.m_sockerRev[ix]->Send(strSeverClose,sizeof(strSeverClose)); 
//		int nsend=CSocket::Send(strSeverClose,sizeof(strSeverClose),0); 
		socketShutdown=m_socketListen.m_sockerRev[ix]; 
		socketShutdown->Send(strSend,nLen); 
	} 
 
} 
CString CSever1_2Dlg::FormatStr(CSocketMsg* pMsg) 
{ 
	CString str; 
	str+=pMsg->Time; 
	str+="  ["; 
	str+=pMsg->ClientID; 
	str+="] :"; 
	str+=13; 
	str+=10; 
	str+=pMsg->SendMsg; 
	str+=13; 
	str+=10; 
	return str; 
} 
CString CSever1_2Dlg::GetCurTime() 
{ 
	CString strtime; 
	CTime time=CTime::GetCurrentTime(); 
	strtime=time.Format("%H:%M:%S (%Y %B %d)"); 
	return strtime; 
} 
void CSever1_2Dlg::CloseSocket(SOCKET s) 
{ 
 
	int ix=-1; 
	do { 
		ix++; 
		 
	} while(s!=m_socketListen.m_sockerRev[ix]->m_hSocket); 
	 
	for (;ixIsEmpty()) 
	{ 
		pList->SetImageList(&m_imageList,LVSIL_NORMAL); 
		m_pStringList->AddTail(ClientID); 
		pList->InsertItem(0,ClientID,0); 
		return; 
	} 
	 
	m_pStringList->AddTail(ClientID); 
	pList->InsertItem(m_pStringList->GetCount()-1,ClientID); 
	 
 
	 
 
 
} 
void CSever1_2Dlg::SetNewClient(char* chDlgText, int nDlgLen) 
{ 
	CString sDlg(chDlgText); 
 
	CSocketMsg* sockMsg=new CSocketMsg; 
	sockMsg->FormatSocketMsg(sDlg); 
	CString strID=sockMsg->ClientID; 
	delete sockMsg; 
//	sDlg.Delete(0); 
//	sDlg.Insert(0,SM_CONNECTSEVER); 
	DisDlgText(sDlg,nDlgLen);  
	OnListChange(strID); 
	m_pOnLineTime[m_socketListen.m_nsockNum-1]=CTime::GetCurrentTime(); 
 
} 
void CSever1_2Dlg::CloseClient(char* chDlgText, int nDlgLen) 
{ 
	CString sDlg(chDlgText); 
	 
	CSocketMsg* sockMsg=new CSocketMsg; 
	sockMsg->FormatSocketMsg(sDlg); 
	CString strID=sockMsg->ClientID; 
 
//删除列表上的用户 
	CListCtrl* pList=(CListCtrl*)GetDlgItem(IDC_LIST); 
	POSITION pos=m_pStringList->Find(strID); 
	m_pStringList->RemoveAt(pos); 
 
	CTime curtime=CTime::GetCurrentTime(); 
	CTimeSpan disTime; 
//	pList->DeleteItem(ix); 
	for (int ix=0;ix<=m_socketListen.m_nsockNum;ix++) 
	{ 
	 
		if (pList->GetItemText(ix,0)==strID) 
		{ 
			pList->DeleteItem(ix); 
			pList->UpdateData(); 
			//在线时间 
			disTime=curtime-m_pOnLineTime[ix]; 
			break; 
		} 
	} 
	CString time=disTime.Format("%H时 %M分 %S秒"); 
	sockMsg->SendMsg+=13; 
	sockMsg->SendMsg+=10; 
	sockMsg->SendMsg+="用户"; 
	sockMsg->SendMsg+=sockMsg->ClientID; 
	sockMsg->SendMsg+=": 在这里荒废了:"; 
	sockMsg->SendMsg+=time; 
//	sockMsg->SendMsg+="的时间!!"; 
//	sockMsg->ClientID="Sever"; 
	sockMsg->SystemMsg=SM_SENDMESSAGE; 
	sDlg=sockMsg->FormatMsg(); 
	nDlgLen=sDlg.GetLength(); 
	delete sockMsg; 
	DisDlgText(sDlg,nDlgLen);  
	CString strNum; 
	strNum.Format("%d",m_socketListen.m_nsockNum); 
	SetDlgItemText(IDC_STATIC_NUM,strNum); 
} 
 
void CSever1_2Dlg::OnSavemsg()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog dlg(FALSE,"*.*|*.*",NULL,NULL,"*.dat|*.dat||"); 
	CString strfilename; 
	BOOL bOk=dlg.DoModal(); 
	if(bOk==IDOK) 
	{ 
		strfilename=dlg.GetPathName(); 
		UpdateData(FALSE); 
	} 
	else if (bOk==IDCANCEL) 
		return; 
	CFile fSave; 
	CFileException ex; 
//	char* pFileName="ClientMsg.dat"; 
	if (!fSave.Open(strfilename,CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate,&ex)) 
	{ 
		#ifdef _DEBUG 
			afxDump << "File could not be opened " << ex.m_cause << "\n"; 
		#endif 
	} 
 
	fSave.Write(strReceive,strReceiveLen); 
	fSave.Close(); 
	AfxMessageBox("您已经安全退出服务器,并保存了本次聊天纪录"); 
	OnCancel(); 
} 
void CSever1_2Dlg::SetNewSock(SOCKET s) 
{ 
	 
	int ix=-1; 
	do { 
		ix++; 
		 
	} while(s!=m_socketListen.m_sockerRev[ix]->m_hSocket); 
	CSocketMsg* sockMsg=new CSocketMsg; 
	sockMsg->SystemMsg=SM_CONNECTSEVER; 
	sockMsg->SendMsg="正在导入系统信息!"; 
	sockMsg->Time=GetCurTime(); 
	CString ClientID; 
	CString ClientNext; 
	//查询出每一个在线ID 
	CSocket* socketShutdown=m_socketListen.m_sockerRev[ix]; 
	POSITION pos=m_pStringList->GetHeadPosition(); 
 
	for (int iy=1;iyGetAt(pos); 
		sockMsg->ClientID=ClientID; 
		CString sendMsg=sockMsg->FormatMsg(); 
		int nLen=sendMsg.GetLength(); 
		socketShutdown->Send(sendMsg,nLen); 
		m_pStringList->GetNext(pos); 
	} 
	delete sockMsg; 
	 
}