CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: File Transfer

  1. #1
    Join Date
    Nov 2004
    Posts
    16

    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.

  2. #2
    Join Date
    Aug 2002
    Posts
    879

    Re: File Transfer

    there are many methods
    use the search function "lan file transfer"

  3. #3
    Join Date
    Jan 2004
    Location
    Bangalore
    Posts
    53

    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

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: File Transfer

    Well....what protocol? HTTP? FTP?

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

  6. #6
    Join Date
    Nov 2004
    Posts
    16

    Re: File Transfer

    Quote Originally Posted by Andreas Masur
    Well....what protocol? HTTP? FTP?

    Hello Sir,

    FTP Would b sufficient...

    Expecting ur Advice...

    thanks

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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.

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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.

  9. #9
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

    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

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: File Transfer

    Quote 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?

  11. #11
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

    Re: File Transfer

    Quote 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.

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: File Transfer

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured