|
-
February 11th, 2005, 03:46 AM
#1
File Transfer
Hi All,
Has anyone of u developed a class which can Transfer a File across LAN.
I have a client Server Application which can Send and Receive messages between them. Now i want to implement the File Transfer.
Looking forward to ur valuable Suggestions..
Thanks.
-
February 11th, 2005, 03:53 AM
#2
Re: File Transfer
there are many methods
use the search function "lan file transfer"
-
February 11th, 2005, 04:44 AM
#3
Re: File Transfer
Consider your file as some long message. Read the file and send as a string to the other system, but you ve to retrieve the file name, propeties... and send them also.
In the other (receiver) side create one file with the same name and properties, write the received string into it.
( to identify the file name and properties add some tag with the msg ex: FileName:File1,FileTime:... ).
I guess some modifications to your existing code will do the above thing.
Regards
MJV
-
February 11th, 2005, 07:04 AM
#4
Re: File Transfer
Well....what protocol? HTTP? FTP?
-
February 11th, 2005, 07:10 AM
#5
-
February 11th, 2005, 10:17 PM
#6
Re: File Transfer
 Originally Posted by Andreas Masur
Well....what protocol? HTTP? FTP?
Hello Sir,
FTP Would b sufficient...
Expecting ur Advice...
thanks
-
February 12th, 2005, 03:49 AM
#7
Re: File Transfer
Code:
void CopyFromFTPServer()
{
int iRC = 0;
CFile LocalFile;
CInternetFile *pInternetFile = 0;
CFtpConnection *pFTPConnection = 0;
CInternetSession *pInternetSession = 0;
pInternetSession = new CInternetSession();
if(pInternetSession)
{
try
{
pFTPConnection = pInternetSession->GetFtpConnection(_T("ftp.server.com"), _T("username"), _T("password"));
pInternetFile = pFTPConnection->OpenFile(_T("test.txt"));
LocalFile.Open(_T("c:\\test.txt"), CFile::modeCreate | CFile::modeWrite);
// Read data
TCHAR szBuffer[50] = "";
while((iRC = pInternetFile->Read(static_cast<void *>(szBuffer), sizeof(szBuffer))) != 0)
{
// Write to file
LocalFile.Write(static_cast<const void *>(szBuffer), iRC);
// Clear buffer
memset(szBuffer, 0, sizeof(szBuffer));
}
}
catch(CInternetException* pEx)
{
TCHAR szErrorMessage[1024] = "";
pEx->GetErrorMessage(szErrorMessage, sizeof(szErrorMessage) - 1);
AfxMessageBox(szErrorMessage, "Error");
pEx->Delete();
}
}
// If the connection is open, close it
if(pFTPConnection)
pFTPConnection->Close();
delete pInternetSession;
}
void CopyToFTPServer()
{
int iRC = 0;
CFile LocalFile;
CInternetFile *pInternetFile = 0;
CFtpConnection *pFTPConnection = 0;
CInternetSession *pInternetSession = 0;
pInternetSession = new CInternetSession();
if(pInternetSession)
{
try
{
pFTPConnection = pInternetSession->GetFtpConnection(_T("ftp.server.com"), _T("username"), _T("password"));
pInternetFile = pFTPConnection->OpenFile(_T("test.txt"), GENERIC_WRITE);
LocalFile.Open(_T("c:\\test.txt"), CFile::modeRead);
// Write data
TCHAR szBuffer[50] = "";
while((iRC = LocalFile.Read(static_cast<void *>(szBuffer), sizeof(szBuffer))) != 0)
{
// Write to file
pInternetFile->Write(static_cast<const void *>(szBuffer), iRC) ;
// Clear buffer
memset(szBuffer, 0, sizeof(szBuffer));
}
}
catch(CInternetException* pEx)
{
TCHAR szErrorMessage[1024] = "";
pEx->GetErrorMessage(szErrorMessage, sizeof(szErrorMessage) - 1);
AfxMessageBox(szErrorMessage, "Error");
pEx->Delete();
}
}
// If the connection is open, close it
if(pFTPConnection)
pFTPConnection->Close();
delete pInternetSession;
}
Last edited by Andreas Masur; February 12th, 2005 at 05:03 AM.
-
February 12th, 2005, 03:50 AM
#8
Re: File Transfer
The following is a download class which downloads a file from a FTP-Server in its own thread...
Code:
#include <memory>
class CFTPDownload
{
public:
CFTPDownload(const char *Server,
const char *ServerFileName,
const char *LocalFileName,
const char *User,
const char* Password)
{
m_Server = Server;
m_ServerFileName = ServerFileName;
m_ServerFileName = LocalFileName;
m_User = User;
m_Password = Password;
// Create thread
m_Thread = ::CreateThread(0, 0, Thread, this, 0, 0);
}
virtual ~CFTPDownload() { ::CloseHandle(m_Thread); }
CString &GetCause() { return Cause; }
private:
HANDLE m_Thread;
CString m_Server;
CString m_User;
CString m_Password;
CString m_ServerFileName;
CString m_LocalFileName;
CString m_Cause;
static UINT Thread(LPVOID Param);
};
UINT CFTPDownload::Thread(LPVOID Param)
{
CFTPDownload *Parent = static_cast<CFTPDownload *>(Param);
int RC = 0;
bool Success = true;
CFile LocalFile;
CInternetFile *InternetFile = 0;
CFtpConnection *FTPConnection = 0;
std::auto_ptr<CInternetSession> InternetSession(new CInternetSession());
if(InternetSession.get())
{
try
{
FTPConnection = InternetSession->GetFtpConnection(Parent->m_Server, Parent->m_User, Parent->m_Password);
InternetFile = FTPConnection->OpenFile(Parent->m_ServerFileName);
LocalFile.Open(Parent->m_LocalFileName, CFile::modeCreate | CFile::modeWrite);
// Read data
TCHAR Buffer[50] = "";
while((RC = InternetFile->Read(static_cast<void*>(Buffer), sizeof(Buffer))) != 0)
{
// Write to file
LocalFile.Write(static_cast<const void*>(Buffer), RC);
// Clear buffer
memset(Buffer, 0, sizeof(Buffer));
}
}
catch(CInternetException* Ex)
{
TCHAR Error[1024] = "";
Ex->GetErrorMessage(Error, sizeof(Error) - 1);
Parent->m_Cause = Error;
Ex->Delete();
Success = false;
}
}
// If the connection is open, close it
if(FTPConnection)
FTPConnection->Close();
return Success;
}
In your application you can then start a download with the following...
Code:
CFTPDownload DownloadOne("ftp.server.com", "File_A.zip", "c:\\file_a.zip", "user", "password");
CFTPDownload DownloadTwo("ftp.server.com", "File_B.zip", "c:\\file_b.zip", "user", "password");
It is of course just a small sample to give you the idea...there is no functionality added like signaling the finishing of the download etc.
Last edited by Andreas Masur; February 12th, 2005 at 05:01 AM.
-
February 12th, 2005, 04:08 AM
#9
Re: File Transfer
If you are using MFC you can use the code as posted above [provide you correct the lines like]
Code:
LocalFile.Open(Parent->m_LocalFileName, CFile::modeWrite);
If you do not want use the MFC wrappers you can use the WIN32 APIs WinInet
-
February 12th, 2005, 04:41 AM
#10
Re: File Transfer
 Originally Posted by Mick
If you are using MFC you can use the code as posted above [provide you correct the lines like]
Code:
LocalFile.Open(Parent->m_LocalFileName, CFile::modeWrite);
Correct with what?
-
February 12th, 2005, 04:45 AM
#11
Re: File Transfer
 Originally Posted by Andreas Masur
Correct with what?
Well I realize it is a sample...but I do not think whatever local file they are writing to will already exists.
-
February 12th, 2005, 05:00 AM
#12
Re: File Transfer
 Originally Posted by Mick
Well I realize it is a sample...but I do not think whatever local file they are writing to will already exists.
Okay....you mean it would be wise if one includes 'CFile::modeCreate' as well...? Done...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|