/* byte* data = new byte[fileLengths]; recSo.Receive(data,fileLengths); CFile fs(filemes,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary); //存文件 fs.Write(data,fileLengths+1);*/
char bufs[1024]; //缓冲SIZEFILE=1024 //byte* data = new byte[1024]; CFile fs(filemes,CFile::modeCreate|CFile::modeWrite |CFile::typeBinary); //存文件 int n=0; //接受的字节数 0表示结束 int temp=0;
{
// TODO: Add your control notification handler code here
AfxSocketInit();
// CSocket recso,seversocket;
SOCKADDR_IN client;
if(!recso.Socket())
{
MessageBox("初始化失败");
return;
}
BOOL boptvalue=TRUE;
int boptlen=sizeof(BOOL);
recso.SetSockOpt(SO_REUSEADDR,(void *)&boptvalue,boptlen,SOL_SOCKET);
if (!recso.Bind(11024))
{
MessageBox("绑定失败");
return;
}
if (!recso.Listen(10))
{
MessageBox("监听失败");
return;
}
if (!recso.Accept(seversocket))
{
MessageBox("接受失败");
return;
}
else
{
//client=seversocket.
while (1)
{
saveYouFiles(seversocket,client);
}
}
}
int CFilerecieveDlg::saveYouFiles(CSocket &recSo, SOCKADDR_IN &client)
{
CString fnames;
CString filenamepath;
//static int filescount=0;
CString strfileIp,strfileName,strfileLength;
char filemes[512]={0};//存文件名字的
int fileLengths=0;
FOLDERINFO myFileInfo;
recSo.Receive(&myFileInfo,sizeof(FOLDERINFO));
fileLengths=myFileInfo.fileLength;
/*strfileIp.Format(inet_ntoa(client.sin_addr));*/
strfileName.Format(myFileInfo.fileName); //得到文件名字
strfileLength.Format("%f",myFileInfo.fileLength/1024.0);
filenamepath.Format(myFileInfo.filePath); //得到文件路径
int a=filenamepath.ReverseFind(_T('\\')); //这5行代码是用来得到一个例如:\AMD\Intel\ATI\ filenamepath=filenamepath.Left(a+1); //主要是用来建立目录
CString b=filenamepath;
a=b.ReverseFind(_T(':'));
b=b.Right(b.GetLength()-a-1);
filenamepath="files"+b; //所有的文件都存在files文件夹下
CreateDirectory(filenamepath,NULL); //创建目录
strcpy(filemes,filenamepath+strfileName);
//strcpy(filemes,filenamepath);
/*
byte* data = new byte[fileLengths];
recSo.Receive(data,fileLengths);
CFile fs(filemes,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary); //存文件
fs.Write(data,fileLengths+1);*/
char bufs[1024]; //缓冲SIZEFILE=1024
//byte* data = new byte[1024];
CFile fs(filemes,CFile::modeCreate|CFile::modeWrite |CFile::typeBinary); //存文件
int n=0; //接受的字节数 0表示结束
int temp=0;
//开始接收
for(;;)
{
n=recSo.Receive(bufs,1024); //接受
if(n==0) //0表示结束
break; //接受完毕
fs.Write(bufs,n);
// fputs(bufs,fp);
temp+=n;
}
// delete []data;
fs.Close();
//recSo.Close();
return 0;
}
struct FOLDERINFO {
int fileLength; //记录文件长度
char fileName[128]; //记录文件名
char filePath[512]; //记录文件路径
byte* data;
};
void CFilesendDlg::OnButton1()
{
// TODO: Add your control notification handler code here
AfxSocketInit();
// CSocket sockClient;
sockClient.Create(); //创建Socket
sockClient.Connect("127.0.0.1", 11024); //
Recurse(_T("E:\\test"));
sockClient.Close();
}void CFilesendDlg::Recurse(LPCTSTR pstr)
{
CFileFind finder;
/*
AfxSocketInit();
CSocket sockClient;
sockClient.Create(); //创建Socket
sockClient.Connect("127.0.0.1", 11024); //*/
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
CString sFileName = finder.GetFileName();
CString filepath=finder.GetFilePath();
//cout << (LPCTSTR)sFileName << endl;//输出查找文件夹下的所有文件名
if (finder.IsDirectory())
{
Recurse(_T(filepath));
return;
}
///////////////////////////////////////////////////////////////////
CFile myFile;
FOLDERINFO myFileInfo;
if(!myFile.Open(_T(filepath/*"E:\\test\\dixin.txt"*/),CFile::modeRead|CFile::typeBinary,NULL))//m_fileName是遍历后得到的文件
{
return ;
}
myFileInfo.fileLength=myFile.GetLength(); //得到文件大小
strcpy(myFileInfo.fileName,myFile.GetFileName()); //得到文件名称
strcpy(myFileInfo.filePath,filepath/*"E:\test\dixin.txt"*/);
//myFileInfo.myfilesend=&myFile;
sockClient.Send(&myFileInfo,sizeof(myFileInfo)); //发送文件信息
/*
byte* data = new byte[myFile.GetLength()];
myFile.Read(data, myFile.GetLength());
sockClient.Send(data, myFile.GetLength());*/
myFile.Seek(0,CFile::begin);
char m_buf[1024]={0}; //缓冲区
CString strError;
int num=0;
int temp=0;
int end=0;
//开始发送
for(;;)
{
num=myFile.Read(m_buf, 1024);
if(num==0) break;
end=sockClient.Send(m_buf, num);
temp+=end;
}
//delete []data;
myFile.Close();
// return ;
}
//sockClient.Close();
finder.Close();}
不知是否是你想要 的