📄 testclientdlg.cpp
字号:
void CTestclientDlg::OnReceive()
{ UpdateData(TRUE);
char *pBuf = new char[1025];
CString cBuf;
int iBufSize = 1024;
int iRcvd;
int iSent1 ;
int iLen1;
int count,num,kind;
CString SendAck;
theTime = CTime::GetCurrentTime();
strStatusMsg = theTime.Format("%H:%M:%S"); //记录事件发生的时间
iRcvd = m_sConnectSocket.Receive(pBuf,iBufSize);
pBuf[iRcvd] = NULL; //收到的字符串,char 型
strBuf = pBuf; //收到的字符串,CString型
if(iRcvd == SOCKET_ERROR)
{
m_ctrStatus.AddString(strStatusMsg + " 网络传输有误!");
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
}
else
{
KillTimer(2); //证明收到了回应,关掉定时器
iReSend = 0; //重发次数清空
if(iConnStatus == 1 || strRecvd == "client ok.server ok.") //当系统状态为: 等待客户端联接
{
iConnStatus = 2; //设置成功联接客户端标志
//iFramePos = 0;
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE); //启用“发送消息”按钮
GetDlgItem(IDC_EMSG)->EnableWindow(TRUE); //启用“发送消息”文本框
m_ctrStatus.AddString(strStatusMsg+" 服务器成功响应."); //写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
}
else if(iConnStatus == 2) //当系统状态为: 准备就绪, 并接收到第一帧数据
{ //GetDlgItem(IDC_BSEND)->EnableWindow(FALSE); //禁用“发送消息”按钮
//GetDlgItem(IDC_EMSG)->EnableWindow(FALSE); //禁用“发送消息”文本框
sscanf(strBuf, "共%d帧-第%d帧-%d类帧-%s", &count, &num,&kind,cBuf); //提取出收到的帧中的编号和字符,以*分隔开。
switch(kind)
{
case 1: if(frame_expected == num)
{
SendAck.Format(_T("共%d帧-第%d帧-%d类帧-%s"), count,num,2,"返回帧");
iLen1 = SendAck.GetLength();
iSent1 = m_sConnectSocket.Send(LPCTSTR(SendAck),iLen1);
if (iSent1 == SOCKET_ERROR)
{SetTimer(1000,2000, NULL);} // 发送失败或超时
else
{
if(count == num+1)
{
m_ctrStatus.AddString(strStatusMsg+" 收到最后一帧:"+strBuf);
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE); //“发送消息”按钮
GetDlgItem(IDC_EMSG)->EnableWindow(TRUE); //“发送消息”文本框
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);
strRecvd.Insert(num*16, cBuf);
strRecvd.Insert((num+1)*16, '\0');
frame_expected++;
SetDlgItemText(IDC_EDIT_RECVMI,strRecvd);
//m_recvmi.SetWindowText(strRecvd);
}
else
{
m_ctrStatus.AddString(strStatusMsg+" 收到帧:"+strBuf); //写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);
strRecvd.Insert(num*16, cBuf);
strRecvd.Insert((num+1)*16, '\0');
frame_expected++;
//SetDlgItemText(IDC_EDIT_RECVMI,strRecvd);
//m_recvmi.SetWindowText(strRecvd);
}
}
}
break;
case 2:
if(ack_expected == num)
{
if(count == num +1)
{
event = 0;Sleep(1);
m_ctrStatus.AddString(strStatusMsg+" 收到最后一帧的返回帧:"+strBuf); //写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);
KillTimer(ack_expected);
ack_expected =0;
next_frame_to_send = 0;
nbuffered =0;
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE); //禁用“发送消息”按钮
GetDlgItem(IDC_EMSG)->EnableWindow(TRUE); //禁用“发送消息”文本框
}
else
{
m_ctrStatus.AddString(strStatusMsg+" 收到返回帧:"+strBuf); //写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);
ack_expected++;
nbuffered--;
KillTimer(ack_expected-1);
}
}
}
}
}
UpdateData(FALSE);
return ;
}
//断开联接函数
void CTestclientDlg::OnDisConnect()
{
// TODO: Add your control notification handler code here
//关闭Socket联接
UpdateData(TRUE);
m_sConnectSocket.Close();
theTime = CTime::GetCurrentTime();
strStatusMsg = theTime.Format("%H:%M:%S"); //记录事件发生的时间
m_ctrStatus.AddString(strStatusMsg + " 已断开连接!\n");
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
iConnStatus = 0;
//启用"联接服务端"按钮, 禁用"断开联接"和"发送消息"按钮
GetDlgItem(IDC_Connect)->EnableWindow(TRUE);
GetDlgItem(IDC_BSEND)->EnableWindow(FALSE);
GetDlgItem(IDC_DisConnect)->EnableWindow(FALSE);
UpdateData(FALSE);
}
void CTestclientDlg::OnBsend() //发送消息函数
{
// TODO: Add your control notification handler code here
next_frame_to_send=0;
nbuffered = 0;
event =1;
int iLen;
int iSent=0;
UpdateData(TRUE);
theTime = CTime::GetCurrentTime();
strStatusMsg = theTime.Format("%H:%M:%S"); //记录事件发生的时间
if(m_strMsg !="" || iConnStatus == 3)
{ if(iConnStatus <= 1) //系统断连状态
{
m_ctrStatus.AddString(strStatusMsg+" 用户已中止发送"); //写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
strSendMsgBuffer = "";
}
else if(iConnStatus == 2) //系统就绪, 开始发送第一帧数据
{ //iFramePos = 0;
//iReSend = 0;
m_ctrStatus.AddString(strStatusMsg+" 系统进入发送状态!"); //写日志
GetDlgItem(IDC_BSEND)->EnableWindow(FALSE); //禁用“发送消息”按钮
GetDlgItem(IDC_EMSG)->EnableWindow(FALSE); //禁用“发送消息”文本框
AfxBeginThread(Sender,this);
}
}
UpdateData(FALSE);
return;
}
void CTestclientDlg::OnResend() //消息重发函数
{
int iLen;
int iSent=0;
theTime = CTime::GetCurrentTime();
strStatusMsg = theTime.Format("%H:%M:%S"); //记录事件发生的时间
iReSend = iReSend + 1; //重发次数递增
if(iReSend >= 5) //重发超过5次的上限,停止发送,并恢复系统设置
{
iReSend = 0; //初始化重发计数器
iConnStatus = 2; //系统改为初始状态
m_ctrStatus.AddString(strStatusMsg+" 重发过多,中止!"); //写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE); //启用“发送消息”按钮
GetDlgItem(IDC_EMSG)->EnableWindow(TRUE); //启用“发送消息”文本框
UpdateData(FALSE);
return;
}
else
{
m_ctrStatus.AddString(strStatusMsg+" 重发当前帧:"+strSendMsgBuffer);
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
CString ReSendData;
ReSendData.Format(_T("共%d帧-第%d帧-%d类帧-%s"), m_strMsg.GetLength()/16,next_frame_to_send,1,m_strMsg.Mid(next_frame_to_send*16,16)); //拼凑一帧信息:帧号*信息总长度*数据
iLen = ReSendData.GetLength();
iSent = m_sConnectSocket.Send((LPCTSTR)ReSendData,iLen); //发送帧
if (iSent == SOCKET_ERROR){SetTimer(1001,2000, NULL);} //发送失败或超时
else {SetTimer(next_frame_to_send,2000, NULL);} //看对应返回是否超时
}
}
void CTestclientDlg::OnTimer(UINT_PTR nIDEvent) //定时器处理事件
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
KillTimer(nIDEvent); //关掉定时器
theTime = CTime::GetCurrentTime();
strStatusMsg = theTime.Format("%H:%M:%S"); //记录事件发生的时间
if(nIDEvent<1000)
{cMutex.Lock();
next_frame_to_send = nIDEvent;
nbuffered--;
OnResend();
cMutex.Unlock();
}
else{
switch (nIDEvent)
{
case 1001: //处理ID为1的定时器
m_ctrStatus.AddString(strStatusMsg+" 发送失败!(超时或断开)"); //发送失败, 写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
UpdateData(FALSE);
//this->OnResend(); //重发
break;
case 1000: //处理ID为0的定时器
m_ctrStatus.AddString(strStatusMsg+" 端口不可用或被防火墙阻止"); //写日志
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
break;
case 1002:
m_ctrStatus.AddString(strStatusMsg+" 未收到返回帧!(超时)"); //写日志
//this->OnResend(); //重发
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1); //垂直滚动到底部
break;
}}
//CDialog::OnTimer(nIDEvent);
}
CString CTestclientDlg::GetErrorMsg()
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,0, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //默认语言
(LPTSTR)&lpMsgBuf, 0, NULL );
//显示
//MessageBox(0,(LPCTSTR)lpMsgBuf,_T("GetLastError"),MB_OK|MB_ICONINFORMATION );
CString m_strError;
m_strError.Format("错误代码:%d \n出错信息:%s",GetLastError(), (LPCTSTR)lpMsgBuf);
//释放内存
::LocalFree( lpMsgBuf );
return m_strError;
}
void CTestclientDlg::OnButtonOk()
{
CString strMsg,strData,strTemp,strTemp1;
GetDlgItemText(IDC_EDIT_MSG,strMsg);
SetDlgItemText(IDC_EDIT_MSG,"");
strSendming=strMsg;
if(strMsg.IsEmpty()==TRUE)
{
MessageBox("不能发送空消息");
}
else
{
GetDlgItemText(IDC_EDIT_SENDMING,strTemp);
// SetDlgItemText(IDC_EDIT_SENDMING,strMsg);
COleDateTime dtCurrent;
dtCurrent = COleDateTime::GetCurrentTime();
strData = dtCurrent.Format("%H:%M:%S");
strTemp1 += "你在" + strData;
strTemp1 += "说:" + strMsg;
strTemp1 += "\r\n\r\n";
strTemp1 += strTemp;
SetDlgItemText(IDC_EDIT_SENDMING,strTemp1);
}
}
void CTestclientDlg::OnButtonJiami()
{
CString strSendmi,strData;
HPC Hpc1(miyao,key_len,spice);
Hpc1.Jiami(strSendming,strSendmi);
COleDateTime dtCurrent;
dtCurrent = COleDateTime::GetCurrentTime();
strData = dtCurrent.Format("%H:%M:%S");
m_ctrStatus.AddString(strData + " 加密成功.");
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);
// strSysmsg +=strData + ":" + "\r\n" + "加密成功!" + "\r\n";
// SetDlgItemText(IDC_EDIT_SYSMSG,strSysmsg);
SetDlgItemText(IDC_EDIT_SENDMI,strSendmi);
}
void CTestclientDlg::OnButtonJiemi()
{
CString strRecvming,temp,strData,str,strRecvmi;
COleDateTime dtCurrent;
dtCurrent = COleDateTime::GetCurrentTime();
strData = dtCurrent.Format("%H:%M:%S");
HPC Hpc1(miyao,key_len,spice);
GetDlgItemText(IDC_EDIT_RECVMI,strRecvmi);
Hpc1.jiemi(strRecvmi,strRecvming);
str += strData+"收到:" + strRecvming;
GetDlgItemText(IDC_EDIT_RECVMING,temp);
str +="\r\n\r\n";
str +=temp;
SetDlgItemText(IDC_EDIT_RECVMING,str);
m_ctrStatus.AddString(strData + " 解密成功.");
m_ctrStatus.SetCurSel(m_ctrStatus.GetCount()-1);
//strSysmsg +=strData + ":" + "\r\n" + "解密成功!" + "\r\n";
//SetDlgItemText(IDC_EDIT_SYSMSG,strSysmsg);
}
void CTestclientDlg::OnButtonSetmiyao()
{
CString b ;
GetDlgItemText(IDC_EDIT_MIYAO,b);
int l = b.GetLength();
if(l<16)
{
MessageBox("密钥过短,至少需要128位");
}
else{
int i,j;
unsigned long ch[33];
memset(ch,0,132);
for(i=0;i<l;i++)
{
ch[i]=b.GetAt(i);
}
memset(miyao,0,32);
for(j=0;j<(l-1)/4+1;j++)
{
for(i=0;i<4;i++)
{
miyao[j]=miyao[j]<<8;
miyao[j]=ch[j*4+i]|miyao[j];
}
}
key_len = l*8;
}
OnButtonSetmiyao2() ;
}
void CTestclientDlg::OnButtonSetmiyao2()
{
CString b ;
GetDlgItemText(IDC_EDIT_MIYAO2,b);
memset(spice,0,64);
int l = b.GetLength();
int i,j,k;
unsigned long ch[65];
memset(ch,0,260);
for(i=0;i<l;i++)
{
ch[i]=b.GetAt(i);
}
for(k=0;k<8;k++)
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
spice[k][j]=spice[k][j]<<8;
spice[k][j]=ch[k*8+j*4+i]|spice[k][j];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -