Solved

FTP Hangs

Posted on 2002-05-23
10
591 Views
Last Modified: 2008-02-07
I am using a method to ftp files, it works very well when uploading one or two files, but when i upload multiple files in a loop at about the 3rd or 4th file it often hangs and i have to end task.
What could i be doing wrong?
Is there a better method i could consider?

Regards


Here is the FTP Code

Public Function MMputFtpFile(hostname As String, UserName As String, UserPassword As String, HostFilename As String, localFilename As String, Optional TransferMode As eFtpTransferType = FTP_TRANSFER_TYPE_ASCII) As Long
    Dim hInternet As Long
    On Error GoTo e
    Dim hFTP As Long
    Dim sDir As String
    Dim l As Long
    hInternet = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
    hFTP = InternetConnect(hInternet, hostname, INTERNET_DEFAULT_FTP_PORT, UserName, UserPassword, INTERNET_SERVICE_FTP, 0, 0)
    If hFTP <> 0 Then
        Debug.Print HostFilename
        Debug.Print localFilename
        l = InStrRev(HostFilename, "/")
        sDir = Left(HostFilename, l)
        FtpCreateDirectory hFTP, "/FastTrackTeam"
        FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads"
        FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads/" & MyUser.UserID
        FtpCreateDirectory hFTP, sDir
        MMputFtpFile = FtpPutFile(hFTP, localFilename, HostFilename, INTERNET_FLAG_DONT_CACHE + TransferMode, 0)
        DoEvents
        Call InternetCloseHandle(hInternet)
        DoEvents
    Else
        MMputFtpFile = 0
    End If
Exit Function
e:
    HandleError Err, "MMPutFTPFILE"
End Function


-----
Here is the loop


For Each mes In m_colMessages
       
        lTemp = 0
        For Each f In mes.Files
            lTemp = lTemp + 1
            lblStatus.Caption = "Uploading " & lTemp & " Of " & mes.Files.Count
            DoEvents
            MMputFtpFile g_FTP_SERVER, g_FTP_USERID, g_FTP_PASS, f.RemotePath, f.Path
            DoEvents
        Next f
       Next mes
0
Comment
Question by:crazyman
10 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 7029483
Do you know where it hangs
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7029495
I had a problem like this that I solved by closing the connection and re-opening it.
0
 
LVL 13

Author Comment

by:crazyman
ID: 7029585
Hangs on the actual FtpPutFile line
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7029634
have you tried closing and re-opening the connection?
0
 
LVL 13

Author Comment

by:crazyman
ID: 7029649
according to AllApi.net
calling
Call InternetCloseHandle(hInternet)
will cause any lower level connections to be dropped.
so i do open and close the connection each time...
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 7029686
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7029780
This is probably unrelated, but you should also explicitly close the handle to the FTP server (hFTP).  In addition you are only closing the handle connection to the Internet (hInternet) when you successfully open a handle to the FTP server.  So perhaps your code should be:

hInternet = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
If hInternet Then
   hFTP = InternetConnect(hInternet, hostname, INTERNET_DEFAULT_FTP_PORT, UserName, UserPassword, INTERNET_SERVICE_FTP, 0, 0)
   If hFTP <> 0 Then
      Debug.Print HostFilename
      Debug.Print localFilename
      l = InStrRev(HostFilename, "/")
      sDir = Left(HostFilename, l)
      FtpCreateDirectory hFTP, "/FastTrackTeam"
      FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads"
      FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads/" & MyUser.UserID
      FtpCreateDirectory hFTP, sDir
      MMputFtpFile = FtpPutFile(hFTP, localFilename, HostFilename, INTERNET_FLAG_DONT_CACHE + TransferMode, 0)
      DoEvents
      InternetCloseHandle hFTP
      DoEvents
   Else
      MMputFtpFile = 0
   End If
   InternetCloseHandle hInternet
Else
   ' Could not get a handle to the internet
End If

Again, probably totally unrelated.

Anthony
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7030083
Crazy, take in mind what emoreau's saids: Close and open the connection.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7030147
Richie_Simonetti, I had overlooked (and you may have too) the code. If you look carefully, you will find that the connection is already open and closed.
0
 
LVL 13

Author Comment

by:crazyman
ID: 7031413
Well i have changed the code to use the class that from freevbcode, this seems to work much better and of course i now have the bonus of a progress bar.


Thanks to all who commented on this question, points go to emoreau.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question