UINT ReceiveDataThread(LPVOID lpParam)
{
CTzg004Dlg *pDlg=(CTzg004Dlg *)lpParam;
//保存文件对话框
CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"所有文件 (*.*)|*.*||");
while(dlg.DoModal()!=IDOK)
{
AfxMessageBox("选择文件出错,请重新选择!");
}
CString str,str1,str2;
CSocket sockRecv;
sockRecv.Create();
pDlg->m_CtrlIPSend.GetWindowText(str);//得到发送端IP地址
pDlg->GetDlgItem(IDC_BUTTON_RECEIVE)->EnableWindow(FALSE);//禁止接收按钮
while(sockRecv.Connect(str,pDlg->m_iDataPort2)==0)//连接发送方地址,若上网,可改为实际IP地址,端口要跟Server端相同。
{
Sleep(50);
}
pDlg->GetDlgItem(IDC_BUTTON_REC_END)->EnableWindow(TRUE);//打开终止接收按钮
str2=dlg.GetPathName();//得到文件名
CFile file;
file.Open(str2, CFile::modeCreate | CFile::modeWrite);
BOOL bFileFail=FALSE;
DWORD dwTemp = 0;
sockRecv.AsyncSelect(0);
sockRecv.IOCtl( FIONBIO, &dwTemp);//变为阻塞方式
UINT uiLength;
sockRecv.Receive(&uiLength, 4);//接收发方(Server端)的文件大小
int iBufSize = 1024 * 5;
int iSize = iBufSize;
LPBYTE pBuf = new BYTE[iBufSize];
int iNumByte;
UINT uiTotal = 0;
while(uiTotal < uiLength)
{
int iEnd=0;
//接收端终止
if(pDlg->m_bRecEnd)
{
AfxMessageBox("接收端终止!");
goto ExitLable2;
}
//接收发送端状态数据
iNumByte=sockRecv.Receive(&iEnd, sizeof(int));
if(iNumByte == SOCKET_ERROR)
{
AfxMessageBox("接收信号错误!");
goto ExitLable2;
}
//发送端终止
if(iEnd==1)
{
AfxMessageBox("发送端终止!");
goto ExitLable2;
}
if((int)(uiLength - uiTotal) < iBufSize)
iSize = uiLength - uiTotal;
int iCount=0;
//读取定长数据
while(iCount<iSize)
{
iNumByte = sockRecv.Receive(pBuf, iSize-iCount);
if(iNumByte == SOCKET_ERROR)
{
AfxMessageBox("接收错误!");
goto ExitLable2;
}
iCount+=iNumByte;
file.Write(pBuf, iNumByte);
}
uiTotal += iCount;//以实际接收字节为准
//设置接收进度
pDlg->m_CtrlProgressRec.SetPos(int(((double)uiTotal/uiLength)*100));
str.Format("接收进度:%d%%",int(((double)uiTotal/uiLength)*100));
//显示接收进度百分比
pDlg->GetDlgItem(IDC_STATIC_REC)->GetWindowText(str1);
if(str1!=str)
pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText(str);
}
//接收文件成功
AfxMessageBox("接收文件成功!");
bFileFail=TRUE;
ExitLable2:
delete[] pBuf;
file.Close();
//文件接收失败,则删除接收文件
if(!bFileFail)
{
CFile::Remove( str2 );
}
sockRecv.Close();
pDlg->m_CtrlProgressRec.SetPos(0);//恢复接收进度
//禁止终止接收按钮
pDlg->GetDlgItem(IDC_BUTTON_REC_END)->EnableWindow(FALSE);
//打开接收按钮
pDlg->GetDlgItem(IDC_BUTTON_RECEIVE)->EnableWindow(TRUE);
//恢复提示进度
pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText("接收进度:");
return 0;
}
本文地址:http://com.8s8s.com/it/it1345.htm