//發送數據
CString strpath="文本文件(*.txt)|*.txt||";
CFileDialog filed(true,NULL,NULL,OFN_HIDEREADONLY,strpath,NULL);
filed.m_ofn.lpstrTitle="打開將要發送的文件";
if(filed.DoModal()!=IDOK)
return;
POSITION pt=filed.GetStartPosition();
CString path=filed.GetNextPathName(pt);
CFile file;
file.Open(path,CFile::modeRead|CFile::typeBinary ); //創建文件讀取對象
file.Seek(0,CFile::begin); //從文件結尾處移動文件指針
int nSize=0;
int nRead=0;
int nSend;
char sBuf[2048]={0};
while(1)
{
nRead=file.Read(sBuf,2048);
if(nRead<=0)
break;
nSize+=nRead;
nSend=send(s,sBuf,nRead,0);
if(nSend!=nRead && nRead>0)
file.Seek(nSize-nRead+nSend,CFile::begin);
}
file.Close();
//接收數據
CString strpath="文本文件(*.txt)|*.txt||";
CString num1="";
CFileDialog filed(false,NULL,NULL,OFN_HIDEREADONLY,strpath,NULL);
filed.m_ofn.lpstrTitle="選擇接收文件所保存的位置";
if(filed.DoModal()==IDOK)
{
POSITION pt=filed.GetStartPosition();
CString path=filed.GetNextPathName(pt);
CFile file;
file.Open(path,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);//創建文件對象
file.Seek(0,CFile::begin); //從文件結尾處移動文件指針
UINT nData=0;
UINT nSize=0;
char rBuf[2048]={0};
while(1)
{
nData=recv(s1,rBuf,2048,0);
if(nData==0 || nData>2048 || (-1 == nData &&GetLastError() != WSAEWOULDBLOCK))
break;
file.Write(rBuf,nData);
file.Flush();
nSize+=nData;
}
file.Close();
大神們,幫我看看是什麼問題啊?
是否循環發送接收 定義自己的格式來解析接收數據長度以及數據包結束標識