第1个回答 2013-07-16
发送端:
CFileDialog Dlg(TRUE);
if(Dlg.DoModal()!=IDOK)
return;
CFile myFile;
if(!myFile.Open(Dlg.GetPathName(), CFile::modeRead | CFile::typeBinary))
{
AfxMessageBox("文件不存在!",MB_OK|MB_ICONERROR);
return;
}
CSocket sockSrvr;
sockSrvr.Create(800);
sockSrvr.Listen();
CSocket sockRecv;
sockSrvr.Accept(sockRecv);
SOCKET_STREAM_FILE_INFO StreamFileInfo;
WIN32_FIND_DATA FindFileData;
FindClose(FindFirstFile(Dlg.GetPathName(),&FindFileData));
//FindClose(FindFirstFile("G:\\maindir",&FindFileData));
memset(&StreamFileInfo,0,sizeof(SOCKET_STREAM_FILE_INFO));
strcpy(StreamFileInfo.szFileTitle,myFile.GetFileTitle());
StreamFileInfo.dwFileAttributes = FindFileData.dwFileAttributes;
StreamFileInfo.ftCreationTime = FindFileData.ftCreationTime;
StreamFileInfo.ftLastAccessTime = FindFileData.ftLastAccessTime;
StreamFileInfo.ftLastWriteTime = FindFileData.ftLastWriteTime;
StreamFileInfo.nFileSizeHigh = FindFileData.nFileSizeHigh;
StreamFileInfo.nFileSizeLow = FindFileData.nFileSizeLow;
sockRecv.Send(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO));
UINT dwRead=0;
while(dwRead<StreamFileInfo.nFileSizeLow)
{
byte* data = new byte[1024];
UINT dw=myFile.Read(data, 1024);
sockRecv.Send(data, dw);
dwRead+=dw;
}
myFile.Close();
sockRecv.Close();
AfxMessageBox("发送完毕!");
接收端:
AfxSocketInit(NULL);
CSocket sockClient;
sockClient.Create();
CString szIP;
GetDlgItemText(IDC_EDIT_IPADDRESS,szIP);
if(!sockClient.Connect((LPCTSTR)szIP, 800))
{
AfxMessageBox("连接到对方机器失败!");
return;
}
SOCKET_STREAM_FILE_INFO StreamFileInfo;
sockClient.Receive(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO));
CString strtitle,str;
strtitle.Format(StreamFileInfo.szFileTitle);
str.Format("G:\\maindir\\cxb\\cxf\\%s",strtitle);
//CFile destFile(StreamFileInfo.szFileTitle, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
DWORD dwAttr = GetFileAttributes("G:\\maindir\\cxb\\cxf");
if(dwAttr == 0xFFFFFFFF) //文件夹不存在
CreateDirectory("G:\\maindir\\cxb\\cxf",NULL);
CFile destFile(str, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
UINT dwRead = 0;
while(dwRead<StreamFileInfo.nFileSizeLow)
{
byte* data = new byte[1024];
memset(data,0,1024);
UINT dw=sockClient.Receive(data, 1024);
destFile.Write(data, dw);
dwRead+=dw;
}
SetFileTime((HANDLE)destFile.m_hFile,&StreamFileInfo.ftCreationTime,
&StreamFileInfo.ftLastAccessTime,&StreamFileInfo.ftLastWriteTime);
destFile.Close();
SetFileAttributes(StreamFileInfo.szFileTitle,StreamFileInfo.dwFileAttributes);
sockClient.Close();
AfxMessageBox("接收完毕!");
}
void CClientDlg::OnChangeEditIpaddress()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
CString szIpAddress;
GetDlgItemText(IDC_EDIT_IPADDRESS,szIpAddress);
if(szIpAddress.IsEmpty())
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
else
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
线程
CreateThread(NULL,0,Thread,(LPVOID)&pathname,0,NULL);
{
}