Socket傳輸文件示例(下)

UINT ReceiveDataThread(LPVOID lpParam)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

         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;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章