Ideas:
Service side
1. First the file is binary open; 2. Transfer the properties of the file to the client; 3. Reads the data from the file in bytes and loops it to the client until it is sent.
Client
1. Collect the file information sent by the server and create the corresponding file; 2. The data information sent by the server is written to the file in bytes; 3. Set some properties of the file.
Corresponding Code:
Data:
typedef struct _SOCKET_STREAM_FILE_INFO {
TCHAR szfiletitle[128]; Title Name of the file
DWORD dwfileattributes; Properties of the file
FILETIME Ftcreationtime; When the file was created
FILETIME Ftlastaccesstime; Last access time of the file
FILETIME Ftlastwritetime; Last modified time of file
DWORD Nfilesizehigh; High-level double word for file size
DWORD Nfilesizelow; The file size of the low double word
DWORD dwReserved0; Reserved, for 0
DWORD dwReserved1; Reserved, for 0
} socket_stream_file_info, * psocket_stream_file_info;
Service side:
void Cserverdlg::onbuttonlicsen ()
{
Todo:add your control notification handler code here
Cfiledialogdlg (TRUE);
if (Dlg.domodal ()!=idok)
Return
CFile MyFile;
if (!myfile.open (dlg.getpathname), Cfile::moderead | Cfile::typebinary))
{
AfxMessageBox ("file does not exist!", mb_ok| MB_ICONERROR);
Return
}
CSocket Socksrvr;
Socksrvr.create (800);
Socksrvr.listen ();
CSocket Sockrecv;
Socksrvr.accept (SOCKRECV);
Socket_stream_file_infostreamfileinfo;
Win32_find_data Findfiledata;
FindClose (FindFirstFile (Dlg.getpathname (), &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;
if (data!=null)
{
Delete[]data;
Data=null;
}
}
Myfile.close ();
Sockrecv.close ();
AfxMessageBox ("Send complete! ");
}
Client:
void Cclientdlg::onbuttonsend ()
{
Todo:add your control notification handler code here
Browseinfo bi;
Char Dispname[max_path], Path[max_path];
Itemidlist * PIDL;
Bi.hwndowner = 0;
Bi.pidlroot = 0;
Bi.pszdisplayname = Dispname;
Bi.lpsztitle = "Select Folder";
Bi.ulflags = Bif_returnonlyfsdirs;
BI.LPFN = 0;
Bi.lparam = 0;
bi.iimage = 0;
if (Pidl = SHBrowseForFolder (&BI))//Popup folder browse window and select Directory
{
if (SHGetPathFromIDList (pidl, path) = = TRUE)
{
}
Else{return;}
}
AfxSocketInit (NULL);
CSocket sockclient;
Sockclient.create ();
Cstringszip;
GetDlgItemText (Idc_edit_ipaddress,szip);
if (!sockclient.connect (LPCTSTR) szip, 800))
{
AfxMessageBox ("Failed to connect to the other machine!");
Return
}
Socket_stream_file_info Streamfileinfo;
Sockclient.receive (&streamfileinfo,sizeof (socket_stream_file_info));
CString Strtmp=streamfileinfo.szfiletitle;
CString Strpath=path;
strpath+= "\";
strpath+=strtmp;
strcpy (Streamfileinfo.szfiletitle,strpath.getbuffer (Strpath.getlength ()));
CFile destfile (streamfileinfo.szfiletitle, 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;
if (data!=null)
{
Delete[]data;
Data=null;
}
}
Setfiletime (HANDLE) Destfile.m_hfile,&streamfileinfo.ftcreationtime,
&streamfileinfo.ftlastaccesstime,&streamfileinfo.ftlastwritetime);
Destfile.close ();
SetFileAttributes (streamfileinfo.szfiletitle,streamfileinfo.dwfileattributes);
Sockclient.close ();
AfxMessageBox ("Reception complete! ");
}
File transfer in VC