FTP doesn't do what I need!! HELP

cirollo

Active Member
Licensed User
Longtime User
Hi guys!

I'm losing a customer because of the damned network library!
For my app is created a csv file and then upload to a FTP in passive mode under 3G network
I'm facing the following situations:

- the file is uploaded, the sub ftp_uploaded completed raise no error (strange that if I put a msgbox in the success event, it never shows!!) but it never arrives on the remote FTP
- the file arrives and is 0 Kb!

my customer is very angry and I don't know how to fix this, the app runs on Android 4.0

please help!

this is my code for creating and sending over FTP the file

B4X:
Sub Panel1_Click 'invio dati
   Dim server As ServerSocket 
   server.Initialize(0, "")
   If server.GetMyIP = "127.0.0.1" Then  'this is the localhost address
    'no connection
      Msgbox2("Nessuna Connessione!",Main.nomeprog,"","Ok","",LoadBitmap (File.DirAssets, "warning_256.png"))
   Else
      Dim Answ As Int
      Answ=Msgbox2("Confermi Invio Dati?",Main.nomeprog,"Si","","No",LoadBitmap (File.DirAssets, "warning_256.png"))
      If Answ=DialogResponse.POSITIVE Then
         TxtLog.Text=" "
         Awake.KeepAlive(True) 'evitiamo lo stand by
         ProgressBar1.Visible = True
         TxtLog.Visible = True
         LblStatus.Visible = True
         LblProgress.Visible = True
         ProgressBar1.Progress = 0
         CheckBox1.Enabled = False
         Panel1.Enabled = False
         Panel2.Enabled = False
         Panel3.Enabled = False
         fileord = ""
         fileinc = ""
         ' generiamo prima il nome del file ordini
         Dim dataexp As String
         Dim now As Long
         now = DateTime.now
         fileord = "ordini"&DateTime.GetYear(now)
         If DateTime.GetMonth(now) < 10 Then
            fileord = fileord&"0"&DateTime.GetMonth(now)
         Else
            fileord = fileord&""&DateTime.GetMonth(now)
         End If
         If DateTime.GetDayOfMonth(now) < 10 Then
            fileord = fileord&"0"&DateTime.GetDayOfMonth(now)
         Else
            fileord = fileord&""&DateTime.GetDayOfMonth(now)
         End If
         If DateTime.GetHour(now) < 10 Then
            fileord = fileord&"0"&DateTime.GetHour(now)
         Else
            fileord = fileord&""&DateTime.GetHour(now)
         End If
         If DateTime.GetMinute(now) < 10 Then
            fileord = fileord&"0"&DateTime.GetMinute(now)
         Else
            fileord = fileord&""&DateTime.GetMinute(now)
         End If
         If DateTime.GetSecond(now) < 10 Then
            fileord = fileord&"0"&DateTime.GetSecond(now)
         Else
            fileord = fileord&""&DateTime.GetSecond(now)
         End If
         fileord = fileord&"_"&IdAge.Trim&".csv"
         dataexp = fileord.SubString2(12,14)&"/"&fileord.SubString2(10,12)&"/"&fileord.SubString2(6,10)
         ' recuperiamo i dati da inserire nel file csv da inviare
         Dim numfiles As Int
         numfiles = 0
         ' ordini
         Dim stringarecord,note_ord As String
         Dim Cursor1 As Cursor
         Dim List1 As List
             List1.Initialize
         Cursor1 = SQL1.ExecQuery("SELECT Seriale,IdOrd,IdCli,IdArt,Um,QtaOrd,QtaOma,ImpUni,Sconto1,Sconto2,Sconto3,Sconto4,AliIva,DataCons,NoteOrd from ORDINI where DataExp = 'NO'")
         If Cursor1.RowCount > 0 Then
            TxtLog.Text = "Creato File "& fileord &Chr(10)&TxtLog.Text
            For i = 0 To Cursor1.RowCount - 1
                 Cursor1.Position = i
               note_ord=Cursor1.GetString("NoteOrd")
               note_ord = note_ord.Replace(Chr(10),"; ") 'normalizzo il multiriga delle note su unica riga
                  stringarecord=Cursor1.GetString("Seriale")&Chr(9)
               stringarecord=stringarecord &Cursor1.GetString("IdOrd")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("IdCli")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("IdArt")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("Um")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("QtaOrd")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("QtaOma")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("ImpUni")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("Sconto1")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("Sconto2")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("Sconto3")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("Sconto4")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("AliIva")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("DataCons")&Chr(9)
               stringarecord=stringarecord &" " &note_ord
               List1.Add(stringarecord)
                ' inseriamo adesso la DataExp sulle righe dell'ordine
               Dim WhereFields As Map
               WhereFields.Initialize
               WhereFields.Put("Seriale", Cursor1.GetString("Seriale"))
               DBUtils.UpdateRecord(SQL1, "Ordini", "DataExp", dataexp, WhereFields)
            Next
            Cursor1.Close
            File.WriteList(sdRoot,fileord,List1)
            numfiles = numfiles + 1
            ProgressBar1.Progress = 25
            If FTPMode="F" Then    'connessione FTP
               TxtLog.Text = "Connessione FTP "& FTPHost &"(" &FTPPort &")" &" in Corso..." &Chr(10)&TxtLog.Text
               TxtLog.Text = "Invio File "& fileord &Chr(10)&TxtLog.Text
               FTP.UploadFile(sdRoot, fileord, True, fileord)
            End If
            If FTPMode="L" Then    'file locali
               ProgressBar1.Progress = ProgressBar1.Progress + 25
            End If
         Else
            numfiles = numfiles
         End If
      
         ' incassi
         fileinc = "incassi"&fileord.SubString(7)
         List1.Initialize
         Cursor1 = SQL1.ExecQuery("SELECT Seriale,IdPar,IdCli,ImpInc,DataInc from PARAPE where DataExp = 'NO' and ImpInc <>'0'")
         If Cursor1.RowCount > 0 Then
            TxtLog.Text = "Creato File "& fileinc &Chr(10)&TxtLog.Text
            For i = 0 To Cursor1.RowCount - 1
                 Cursor1.Position = i
               stringarecord=Cursor1.GetString("Seriale")&Chr(9)
                  stringarecord=stringarecord &Cursor1.GetString("IdPar")&Chr(9)
               stringarecord=stringarecord &Cursor1.GetString("IdCli")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("ImpInc")&Chr(9)
               stringarecord=stringarecord &" " &Cursor1.GetString("DataInc")&Chr(9)
               List1.Add(stringarecord)
                ' inseriamo adesso la DataExp sulle righe degli incassi
               Dim WhereFields As Map
               WhereFields.Initialize
               WhereFields.Put("Seriale", Cursor1.GetString("Seriale"))
               DBUtils.UpdateRecord(SQL1, "Parape", "DataExp", dataexp, WhereFields)
            Next
            Cursor1.Close
            File.WriteList(sdRoot,fileinc,List1)
            numfiles = numfiles + 1
            ProgressBar1.Progress = 75
            If FTPMode="F" Then    'connessione FTP
               TxtLog.Text = "Invio File "& fileinc &Chr(10)&TxtLog.Text
               FTP.UploadFile(sdRoot, fileinc, True, fileinc)
            End If
            If FTPMode="L" Then    'file locali
               ProgressBar1.Progress = ProgressBar1.Progress + 25
            End If
         Else   
            numfiles=numfiles
         End If
      
         If numfiles = 0 Then    'nè ordini nè incassi
            Msgbox2("Non ci Sono Dati da Inviare!",Main.nomeprog,"","Ok","",LoadBitmap (File.DirAssets, "warning_256.png"))
            LblStatus.Visible = False
            LblProgress.Visible = False
            ProgressBar1.Visible = False
            TxtLog.Visible = False
            Awake.ReleaseKeepAlive 'togliamo lo standby
            CheckBox1.Enabled = True
            Panel1.Enabled = True
            Panel2.Enabled = True
            Panel3.Enabled = True
            Activity.Finish
            StartActivity(Main)
         Else
            ProgressBar1.Progress = 100
            Timer1.Initialize("Timer1",4000)
            Timer1.Enabled=True
         End If   
      End If
   End If
End Sub

Sub Timer1_Tick
   Msgbox2("Invio completato!"&CRLF&"E' Necessario Uscire e Rientrare nel programma",Main.nomeprog,"","Ok","",LoadBitmap (File.DirAssets, "warning_256.png"))
   If FTPMode="F" Then    
      'chiudo la connessione FTP
      FTP.Close
   End If
   File.Delete(sdRoot,fileord.Trim)
   File.Delete(sdRoot,fileinc.Trim)
   LblStatus.Visible = False
   LblProgress.Visible = False
   ProgressBar1.Visible = False
   TxtLog.Visible = False
   Awake.ReleaseKeepAlive 'togliamo lo standby
   CheckBox1.Enabled = True
   Panel1.Enabled = True
   Panel2.Enabled = True
   Panel3.Enabled = True
   Activity.Finish
'   StartActivity(Main)
   ExitApplication
End Sub

Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
    Dim s As String
    s = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
   LblProgress.Text = s
End Sub

Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
   If Success = True Then
      ProgressBar1.Progress = ProgressBar1.Progress + 25
'      If ProgressBar1.Progress = 50 Then
         ' inseriamo adesso la DataExp sulle righe degli ordini
'         Dim dataexp As String
'         dataexp = fileord.SubString2(12,14)&"/"&fileord.SubString2(10,12)&"/"&fileord.SubString2(6,10)
'         Dim Reader As TextReader
'         Reader.Initialize(File.OpenInput(sdRoot, fileord))
 '           Dim line As String
  '          Dim WhereFields As Map
'         Do While line <> Null 
'                Try
'               line = Reader.ReadLine.SubString2(0,14)
'               WhereFields.Initialize
'               WhereFields.Put("IdOrd", line)
'               DBUtils.UpdateRecord(SQL1, "Ordini", "DataExp", dataexp, WhereFields)
'            Catch
 '                   Log(LastException.Message)
  '              End Try
'         Loop
'      End If
'      If ProgressBar1.Progress = 100 Then
         ' inseriamo adesso la DataExp sulle righe degli incassi
'         Dim Reader As TextReader
'         Reader.Initialize(File.OpenInput(sdRoot, fileinc))
 '           Dim line As String
  '          Dim WhereFields As Map
'         Do While line <> Null 
'                Try
'               line = Reader.ReadLine.SubString2(0,4)
'               line = line.Trim
'               WhereFields.Initialize
'               WhereFields.Put("Seriale", line)
'               DBUtils.UpdateRecord(SQL1, "Parape", "DataExp", dataexp, WhereFields)
'            Catch
 '                   Log(LastException.Message)
'            End Try
'         Loop
'      End If
   Else
      Log(LastException.Message)
      Msgbox2("Errore nell'Invio!",Main.nomeprog,"","Ok","",LoadBitmap (File.DirAssets, "warning_256.png"))
   End If
End Sub
 
Last edited:

cirollo

Active Member
Licensed User
Longtime User
I know...and I'm sorry

I'm sorry but this is not the right attitude to ask a question.

but believe me I don't know how to fix this problem!

I cannot figure out why sometimes an ftp upload doesn't reache the remote server even if the ftp_uploadcompleted don't raise an error!
 
Upvote 0

manios

Active Member
Licensed User
Longtime User
FTP on 3G network may not work at all, because many provider using proxies. I would check that first.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
the timer

What is the purpose of this timer? If I understand your code it closes the FTP after 4 seconds ??

I used it to force the user to wait 4 seconds before doing something else,

in this way i'm sure the upload is over

the problem is that sometimes the file doesn't arrive on the FTP remote server.

Erel, with the upload_completed sub I can check only that the file started the transfer from the device but not that is arrived at destination, right?
Because, If i put a msgbox when success=false, it never shows even when the file is not arrived.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This timer and the fact that you close the connection there seems a bit suspicious.

UploadComplete event should be raised when the transfer is completed. Whether it completed successfully or not.
If Success value is true then the file has arrived completely to its destination.

As I wrote in another post it is possible that the ISP blocks some of the ports. In that case you will need to use Http to upload the file. It is not too complicated.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
Better this way?

This timer and the fact that you close the connection there seems a bit suspicious.

UploadComplete event should be raised when the transfer is completed. Whether it completed successfully or not.
If Success value is true then the file has arrived completely to its destination.

As I wrote in another post it is possible that the ISP blocks some of the ports. In that case you will need to use Http to upload the file. It is not too complicated.

I moved that ftp.close here

B4X:
      Msgbox2("Non ci Sono Dati da Inviare!",Main.nomeprog,"","Ok","",LoadBitmap (File.DirAssets, "warning_256.png"))
            LblStatus.Visible = False
            LblProgress.Visible = False
            ProgressBar1.Visible = False
            TxtLog.Visible = False
            Awake.ReleaseKeepAlive 'togliamo lo standby
            CheckBox1.Enabled = True
            Panel1.Enabled = True
            Panel2.Enabled = True
            Panel3.Enabled = True
            Activity.Finish
            StartActivity(Main)
         Else
            ProgressBar1.Progress = 100
            Timer1.Initialize("Timer1",4000)
            Timer1.Enabled=True
            If FTPMode="F" Then    
               'chiudo la connessione FTP
               FTP.Close
            End If
         End If

maybe is better.
Regarding ISP blocking, in this way the file should never arrive, while it arrives 2 out of 3 times, this is strange!
Is possibile to wait for upload completed to be raised (for example...while....)
Where I can find an http example to upload the file?
 
Upvote 0
Top