基于TCP的网络通信协议设计毕业论文 下载本文

西安工程大学学士学位论文

}

m_lsInfo.InsertItem(n,strTemp); m_lsInfo.SetItemText(n,1,strUser); CTime tm = CTime::GetCurrentTime();

strTemp = tm.Format(_T(\m_lsInfo.SetItemText(n,2,strTemp); m_lsInfo.SetItemText(n,3,strEvent);

6.3 客户端工程设计

6.3.1新建客户端应用程序

同样的,新建一个机遇对话框的客户端应用程序,设计对话框步骤如客户端相似,首先建立“”工程;

客户端对话框界面

22

西安工程大学学士学位论文

6.3.2 客户端窗口代码片段

(1)“登录”控件代码设计: void CDlgTCPClientDlg::OnBtnLogin() { // TODO: Add your control notification handler code here CString usrName=\

m_edUser.GetWindowText(usrName); if (usrName.IsEmpty()) {

AfxMessageBox(\用户名不能为空!\return;

}

if (!m_ClientSocket.Create()) { AfxMessageBox(_T(\创建失败!\ return;

}

if (!m_ClientSocket.Connect(m_strIP,m_nPort)) { AfxMessageBox(_T(\连接失败!\

m_ClientSocket.Close();

23

西安工程大学学士学位论文

return;

}

m_BtnLogin.EnableWindow(FALSE); m_BtnLogOut.EnableWindow(TRUE); m_edUser.EnableWindow(FALSE); CString strTitle,strRet;

m_edUser.GetWindowText(strRet);

strTitle.Format(_T(\客户端----%s---登陆\

SetWindowText(strTitle);

//------------------------------------------------------ NET_PACK pack;

memset(&pack,0,sizeof(NET_PACK)); pack.fun = LOGIN; CString str;

m_edUser.GetWindowText(str);

lstrcpy(pack.senderID,str);

m_ClientSocket.Send(&pack,sizeof(NET_PACK)); }

(2)“退出”控件代码设计:

void CDlgTCPClientDlg::OnBtnLogout() { // TODO: Add your control notification handler code here

MessageBox(\确定退出!\SetWindowText(_T(\客户端\m_edRCVName.SetWindowText(_T(\m_edUser.EnableWindow(TRUE); m_BtnLogin.EnableWindow(TRUE); m_BtnLogOut.EnableWindow(FALSE); m_BtnSend.EnableWindow(FALSE); m_BtnShake.EnableWindow(FALSE); //------------------------------------------- 24

西安工程大学学士学位论文

NET_PACK pack;

memset(&pack,0,sizeof(NET_PACK)); pack.fun = LOGOUT; CString str;

m_edUser.GetWindowText(str); lstrcpy(pack.senderID,str);

m_ClientSocket.Send(&pack,sizeof(NET_PACK)); m_lsFriend.DeleteAllItems();

}

(3)“抖动”控件代码设计: void CDlgTCPClientDlg::OnBtnShake() { // TODO: Add your control notification handler code here NET_PACK pack;

memset(&pack,0,sizeof(NET_PACK)); pack.fun = SHAKE; CString strUser;

m_edUser.GetWindowText(strUser);

lstrcpy(pack.senderID,strUser);

m_edRCVName.GetWindowText(strUser); lstrcpy(pack.RecerverID,strUser);

m_ClientSocket.Send(&pack,sizeof(NET_PACK)); }

(4)“发送”控件代码设计: void CDlgTCPClientDlg::OnBtnSend() {

// TODO: Add your control notification handler code here CString strContent,strUser;

m_edSContent.GetWindowText(strContent); if (strContent.IsEmpty()) 25

西安工程大学学士学位论文

{

NET_PACK pack;

memset(&pack,0,sizeof(NET_PACK)); pack.fun = TRANSMIT;

pack.Len=lstrlen(strContent); lstrcpy(pack.Connect,strContent); m_edUser.GetWindowText(strUser); lstrcpy(pack.senderID,strUser); strContent = strUser;

m_edRCVName.GetWindowText(strUser); lstrcpy(pack.RecerverID,strUser);

m_ClientSocket.Send(&pack,sizeof(NET_PACK)); //-----------------------------------------------------

strContent += _T(\

CTime time = CTime::GetCurrentTime();

strContent += time.Format(_T(\strContent += pack.Connect; strContent += _T(\ }

(4)“好友列表”控件代码设计:

m_edRContent.SetSel(-1);

m_edRContent.ReplaceSel(strContent); }

AfxMessageBox(_T(\聊天内容不能为空!\return;

void CDlgTCPClientDlg::IniCtruls() {

SetWindowText(_T(\客户端\

m_lsFriend.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_FLATSB);

26