4.2.3 客户端界面设计及主要实现代码
登陆界面
void CChatSystemDlg::OnLogin()
{
// TODO: Add your control notification handler code here
//初始化网络
NetInit();
char ClientBuffer[1024];
char ReceiveBuffer[200];
int i_recv, i, err;
UpdateData(TRUE);
//连接至目的主机
err = connect(ClientSocket, (sockaddr *)&m_ClientSockAddr, sizeof(m_ClientSockAddr));
if(err ==SOCKET_ERROR)
{
closesocket(ClientSocket);
MessageBox(_T("服务器连接失败!"), _T("聊天系统"),
MB_ICONINFORMATION);
return;
}
//将用户名和密码封装并发送到服务器进行验证
sprintf(ClientBuffer, "USER_LOGIN~%s,%s\0", m_Username, m_Password);
i = send(ClientSocket, ClientBuffer,strlen(ClientBuffer) + 1, 0);
if (i <= 0)
{
MessageBox("发送错误!", "聊天系统", MB_ICONERROR|MB_OK);
return;
}
//接收验证结果
i_recv = recv(ClientSocket, ReceiveBuffer, sizeof(ReceiveBuffer),0);
if (i_recv > 0)
{
ReceiveBuffer[i_recv] = '\0';
//如果验证成功,保存用户名并显示用户主界面
if (strcmp(ReceiveBuffer, "success") == 0)
{
Session.Format("%s", m_Username);
CUserDlg dlg;
OnCancel();
dlg.DoModal();
}
//否则显示服务器消息
else
{
MessageBox(ReceiveBuffer, _T("聊天系统"), MB_OK | MB_ICONINFORMATION);
}
}
}
注册号码
} 751com.cn
{
// TODO: Add your control notification handler code here
char regBuffer[1024];
char regRecvBuffer[200];
CChatSystemDlg *pDlg = NULL;
UpdateData(TRUE);
//检查用户名和密码是否为空
if (m_UsNameNew.IsEmpty())
{
MessageBox("用户名不能为空!", "聊天系统", MB_OK | MB_ICONINFORMATION);
GetDlgItem(IDC_USNAME_NEW)->SetFocus();
return;
}
if (m_PasswdNew.IsEmpty())
{
MessageBox("密码不能为空!", "聊天系统", MB_OK | MB_ICONINFORMATION);
GetDlgItem(IDC_PASSWD_NEW)->SetFocus();
return;
}
pDlg = (CChatSystemDlg *)AfxGetApp()->m_pMainWnd;
regsock = pDlg->ClientSocket;
//连接到目标主机
int err = connect(regsock, (sockaddr *)&(pDlg->m_ClientSockAddr), sizeof(pDlg->m_ClientSockAddr));
if(err ==SOCKET_ERROR)
{
closesocket(regsock);
MessageBox(_T("服务器连接失败!"), _T("聊天系统"),MB_ICONINFORMATION);
return;
}
//填充要发送的字符串
sprintf(regBuffer, "USER_REGISTER~%s,%s,%s,%s\0",
m_UsNameNew, m_PasswdNew, m_SecurityNew, m_KeyNew);
//发送到服务器,验证并返回消息
int isend = send(regsock, regBuffer, strlen(regBuffer) + 1, 0);
if (isend <= 0)
{
MessageBox("发送错误!", "聊天系统", MB_ICONERROR | MB_OK);
return;
}
isend = recv(regsock, regRecvBuffer, sizeof(regRecvBuffer), 0);
if (isend > 0)
{
regRecvBuffer[isend] = '\0';
if (strcmp(regRecvBuffer, "success") == 0)
{
MessageBox("注册成功!", "聊天系统", MB_ICONINFORMATION | MB_OK);
OnOK();
}
else
{
MessageBox(regRecvBuffer, "聊天系统", MB_ICONINFORMATION | MB_OK);
m_UsNameNew = "";
m_PasswdNew = "";
GetDlgItem(IDC_USNAME_NEW)->SetFocus();
UpdateData(FALSE);
return;
}
}
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ... 下一页 >>