📄 ppqtestdlg.cpp
字号:
{
// TODO: Add your control notification handler code here
//开始传送文件线程
CFileDialog fileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST);
if(fileDialog.DoModal()==IDOK)
{
//得到被选择的文件名
CString strPath =fileDialog.GetPathName();
//设置文件名
this->m_editFilename.SetWindowText(strPath);
this->m_lphParam->strPathName=strPath;
this->m_lphParam->strFileName=fileDialog.GetFileName();
//得到文件的大小
HANDLE fileHandle =::CreateFile(strPath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
int size =::GetFileSize(fileHandle,NULL);
this->m_lphParam->nFileSize =size;
//设置进度
this->m_progress.SetRange32(0,size);
this->m_progress.SetStep(0xFFF);
this->m_progress.SetPos(0);
//显示文件大小
strPath.Format("/%dK",size/1024);
this->m_strFileSize.SetWindowText(strPath);
this->m_strFileLen.SetWindowText("0");
//设置允许传送
this->m_bRun =true;
//初始化传送文件的尺寸
this->m_nSize =0;
//禁止传送按钮
this->m_btnSend.EnableWindow(FALSE);
//禁止重新选择是否校验
this->m_checkCrc.EnableWindow(FALSE);
::AfxBeginThread(CPPQTESTDlg::SendFileThread,(LPVOID)this->m_lphParam);
}
}
UINT CPPQTESTDlg::SendFileThread(LPVOID lpParam)
{
_THREADPARAM hParam =*((_THREADPARAM*)lpParam);
//创建一个对象
DSPFile df;
df.m_strPathName=hParam.strPathName;
df.m_strFileName=hParam.strFileName;
df.m_nFileSize =hParam.nFileSize;
df.m_lpbRun =&hParam.bRun;
df.m_hwnd =hParam.hWnd;
bool bRun =true;
DSP::_DSPPACKET packet(bRun);
//设置等待时间为10分钟
packet.Timeout.tv_sec =600;
packet.SetParameter(false,(hParam.bCrc)?true:false);
//序列化对象
if(packet.WriteObject(hParam.sock,df))
{
//传送完成
::PostMessage(hParam.hWnd,DSPFile::MSG_SENDCOMPLETE,0,0);
}
else
{
//传送失败,可能是对方放弃或传送失败或超时
::PostMessage(hParam.hWnd,DSPFile::MSG_SENDABORT,0,0);
}
//关闭连接
::closesocket(hParam.sock);
::AfxEndThread(0);
return 0;
}
UINT CPPQTESTDlg::ListenThread(LPVOID lpParam)
{
_THREADPARAM hParam =*((_THREADPARAM*)lpParam);
SOCKET listenSock =DSP::GetTCPSocket(34567,hParam.bNet);
if(listenSock==INVALID_SOCKET)
{
::AfxMessageBox("建立监听失败");
return 0;
}
int nResult =0;
sockaddr_in addr;
while(nResult==0)
{
if(::listen(listenSock,5)==0)
{
SOCKET sock =::accept(listenSock,(sockaddr*)&addr,NULL);
hParam.sock =sock;
::AfxBeginThread(CPPQTESTDlg::RecvFileThread,(LPVOID)&hParam);
}
else
break;
}
::closesocket(listenSock);
::AfxEndThread(0);
return 0;
}
UINT CPPQTESTDlg::RecvFileThread(LPVOID lpParam)
{
_THREADPARAM hParam =*((_THREADPARAM*)lpParam);
//初始化接收文件的对象
DSPFile df;
//设置放充的布尔变量,改变该变量的值,可以立即中止传送,初始化为允许接收
hParam.bRun =true;
df.m_lpbRun =&hParam.bRun;
//设置进行消息传递的窗体的句柄
df.m_hwnd =hParam.hWnd;
DSP::_DSPPACKET packet(hParam.bRun);
//设置接收超时时间
packet.Timeout.tv_sec =600;
if(packet.ReadObject(hParam.sock,&df))
{
//如果读取对象成功
::PostMessage(hParam.hWnd,DSPFile::MSG_SENDCOMPLETE,0,0);
}
else
{
//如果读取对象不成功
::PostMessage(hParam.hWnd,DSPFile::MSG_SENDABORT,0,0);
}
//关闭连接socket
::closesocket(hParam.sock);
::AfxEndThread(0);
return 0;
}
LRESULT CPPQTESTDlg::OnSendAbort(WPARAM wParam, LPARAM lParam)
{
//传送放弃
this->m_bRun =false;
//禁止放弃按钮
this->m_btnClose.EnableWindow(FALSE);
if(this->m_btnListen.IsWindowEnabled())
{
//如果没有点击监听按钮
//允许重新选择连接类型
this->m_checkNet.EnableWindow();
}
//允许重新选择是否校验
this->m_checkCrc.EnableWindow();
//允许连接按钮
this->m_btnConnect.EnableWindow();
::AfxMessageBox("接收或传送过程中止");
return 0;
};
LRESULT CPPQTESTDlg::OnSendComplete(WPARAM wParam, LPARAM lParam)
{
//传送文件完成,则禁止放弃按钮
this->m_btnClose.EnableWindow(FALSE);
if(this->m_btnListen.IsWindowEnabled())
{
//如果没有点击监听按钮
//允许重新选择连接类型
this->m_checkNet.EnableWindow();
}
//允许重新选择是否校验
this->m_checkCrc.EnableWindow();
//允许连接按钮
this->m_btnConnect.EnableWindow();
::AfxMessageBox("传送文件完成");
return 0;
};
LRESULT CPPQTESTDlg::OnRecvSect(WPARAM wParam, LPARAM lParam)
{
this->m_progress.StepIt();
this->m_nSize +=wParam;
CString str;
str.Format("%dK",this->m_nSize/1024);
this->m_strFileLen.SetWindowText(str);
return 0;
};
LRESULT CPPQTESTDlg::OnSendSect(WPARAM wParam, LPARAM lParam)
{
this->m_progress.StepIt();
this->m_nSize +=wParam;
CString str;
str.Format("%dK",this->m_nSize/1024);
this->m_strFileLen.SetWindowText(str);
return 0;
}
LRESULT CPPQTESTDlg::OnSetFilenameSize(WPARAM wParam, LPARAM lParam)
{
//设置文件的名字和大小
std::string* pstr =(std::string*)wParam;
this->m_editFilename.SetWindowText(pstr->c_str());
CString str;
str.Format("/%dK",lParam/1024);
this->m_strFileSize.SetWindowText(str);
this->m_progress.SetRange32(0,lParam);
this->m_progress.SetStep(0xFFF);
//初始化接收的文件的尺寸
this->m_nSize =0;
//允许放弃按钮
this->m_btnClose.EnableWindow();
return 0;
}
void CPPQTESTDlg::OnBtnListen()
{
// TODO: Add your control notification handler code here
//点击监听按钮
//设置接收过中放弃按钮的布尔值
this->m_bRun =true;
this->m_lphParam =new _THREADPARAM(this->m_bRun);
//设置接收消息的窗体的句柄
this->m_lphParam->hWnd =this->m_hWnd;
this->m_lphParam->bNet =(this->m_checkNet.GetCheck())?false:true;
//禁止重新选择连接类型
this->m_checkNet.EnableWindow(FALSE);
//禁止监听按钮
this->m_btnListen.EnableWindow(FALSE);
::AfxBeginThread(CPPQTESTDlg::ListenThread,(LPVOID)this->m_lphParam);
}
void CPPQTESTDlg::OnBtnspiderrecv()
{
// TODO: Add your control notification handler code here
SpiderWnd* bs =new SpiderWnd;
//创建窗体
bs->Create(800,10,170,NULL);
bs->ShowWindow(SW_SHOW);
//设置是否为Internet应用,如果是Internet应用,则设置为true
bs->m_spider.m_bInternet =false;
CString strDomain ="12.0.0.1";
bs->m_spider.AddIP(strDomain,34567);
bs->m_spider.AddIP(strDomain,34567);
bs->m_spider.AddIP(strDomain,34567);
bs->m_spider.AddIP(strDomain,34567);
CString strFile ="需要下载的文件名,不包含路径";
CString strPath ="下载后被保存的文件名,包含路径";
bs->m_spider.RunSpider(strFile,strPath,bs->m_hWnd,6);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -