18,356
社区成员
发帖
与我相关
我的任务
分享
for(int i = 0; i <= pDlg->m_strFileName.GetUpperBound(); i++)
{
UINT uiLength = 0;// 文件数据大小
int iBufSize = 1024 * 5; // 发送缓冲大小
int iSize = 0; // 实际发送缓冲大小
int iNumByte = 0; // 一次send实际发送的数据大小
UINT uiTotal = 0; // 目前已发送的数据大小
BYTE *pSendBuf = new BYTE[iBufSize]; // 发送数据信息
CFile file;
CString str = pDlg->m_strDir + pDlg->m_strFileName.GetAt(i);
CFileException ex;
if( !file.Open(pDlg->m_strDir + pDlg->m_strFileName.GetAt(i), CFile::modeRead) )
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
AfxMessageBox("打开文件出错!");
socketSvr.Close();
socketClt.Close();
return 0;
}
uiLength = (UINT)file.GetLength();
socketClt.Send((void *)&uiLength, 4);
while(uiTotal < (UINT)uiLength)
{
// 根据剩余数据大小,决定iSize
iSize = (int)(uiLength - uiTotal) > iBufSize ? iBufSize : (int)(uiLength - uiTotal);
iSize=file.Read((void *)pSendBuf, iSize);//得到读取的字节数
int iCount=0;
while(iCount<iSize)
{
iNumByte = socketClt.Send((void *)pSendBuf, iSize-iCount);
if(iNumByte == SOCKET_ERROR)
{
AfxMessageBox("发送错误!");
socketSvr.Close();
socketClt.Close();
file.Close();
delete[] pSendBuf;
return 0;
}
iCount+=iNumByte;
if(iCount<iSize)
{
pSendBuf += iCount;
}
}
uiTotal += iCount;
}
char szEnd[3]; // 接收OK确认信息
int nRecv = socketClt.Receive((void *)szEnd, 3);
szEnd[nRecv] = '\0';
if(::lstrcmp(szEnd, "OK") != 0)
{
AfxMessageBox("发送文件失败!");
socketSvr.Close();
socketClt.Close();
file.Close();
delete[] pSendBuf;
return 0;
}
file.Close();
delete[] pSendBuf;
}
char szEnd[3]; // 接收OK确认信息
int nRecv = socketClt.Receive((void *)szEnd, 3);
szEnd[nRecv] = '\0';
if(::lstrcmp(szEnd, "OK") != 0)
{
AfxMessageBox("发送文件夹失败!");
socketSvr.Close();
socketClt.Close();
return 0;
}