Problem CheckFTPConnection(

Anfängerfragen zum Programmieren mit PureBasic.
schleicher
Beiträge: 214
Registriert: 30.03.2014 19:57
Computerausstattung: Purebasic 5.70
Wohnort: 18314 Löbnitz

Problem CheckFTPConnection(

Beitrag von schleicher »

Ich habe ein kleinen FTp-Client-Programm geschrieben, was sich mit einem Linux-Ftp-Serber verbindet. Der Server hat ein Timeout von 3min.
Wenn ich nach den 3min CheckFtpConnection(#ftp) aufrufe kommt als Ergebnis aber immer 1 raus und nicht 0, aber der Server führt keine Befehle mehr aus. Ist das Timeout des Servers noch nicht erreicht, dann kommt bei
CheckFtpConnection(#ftp) auch 1 raus. Was läuft da nicht richtig ?

Laut Pb_Hilfe CheckFTPConnection(

Code: Alles auswählen

Gibt einen Wert ungleich Null zurück, wenn die Verbindung noch vorhanden ist, und Null, wenn der Server die Verbindung getrennt hat. 
Purebasic 5.51
Benutzeravatar
juergenkulow
Beiträge: 188
Registriert: 22.12.2016 12:49
Wohnort: :D_üsseldorf-Wersten

Re: Problem CheckFTPConnection(

Beitrag von juergenkulow »

Hallo,

zwei Fragen:

Tritt das Problem auch auf Linux und MacOS auf?
Welchen Zehntausender-Code schickt der Server zurück? https://en.wikipedia.org/wiki/List_of_F ... turn_codes

Code: Alles auswählen

;Warum CheckFTPConnection() testen
;erstellt mit PureBasic 5.51 auf Windows Vista Home Premium SP2 32 Bit 
;offen Linux MacOS Tests
EnableExplicit
InitNetwork()
Define FTPVerbindung=OpenFTP(#PB_Any, "ftp.free.fr", "anonymous", "")
If 0=FTPVerbindung : Debug "Konnte keine Verbindung zu ftp.free.fr aufbauen." : End: EndIf  
Debug "Erfolgreich verbunden. CheckFTPConnection(FTPVerbindung):"+Str(CheckFTPConnection(FTPVerbindung))
Define i=0, s$=""
While i<200 And CheckFTPConnection(FTPVerbindung)
  If ExamineFTPDirectory(FTPVerbindung) : If NextFTPDirectoryEntry(FTPVerbindung) : s$+FTPDirectoryEntryName(FTPVerbindung) : EndIf : FinishFTPDirectory(FTPVerbindung) : EndIf ;tue etwas 
  Debug Str(i)+":"+s$
  If Len(s$)>=180: s$="" : EndIf 
  Delay(1000) ; 1 Sekunde warten 
  i+1
Wend  
Debug "Sekunden:"+ Str(i)+" CheckFTPConnection(FTPVerbindung):"+Str(CheckFTPConnection(FTPVerbindung))  
; Nach 8 Sekudnen wurde der Netzwerkstecker am DSL-Modem gezogen und so die Verbindung unterbrochen. 
; Erfolgreich verbunden. CheckFTPConnection(FTPVerbindung):1
; Sekunden:200 CheckFTPConnection(FTPVerbindung):1
; Aus der Variablenliste: s$ = "tmptmptmptmptmptmptmptmptmp"
;   FTPVerbindung = 25562400
;   i = 190
__________________________________________________
URL-Tags angepasst
23.02.2017
RSBasic
Bitte stelle Deine Fragen, denn den Erkenntnisapparat einschalten entscheidet über das einzig bekannte Leben im Universum.

Jürgen Kulow Wersten :D_üsseldorf NRW D Europa Erde Sonnensystem Lokale_Flocke Lokale_Blase Orion-Arm
Milchstraße Lokale_Gruppe Virgo-Superhaufen Laniakea Sichtbares_Universum
Benutzeravatar
NicTheQuick
Ein Admin
Beiträge: 8679
Registriert: 29.08.2004 20:20
Computerausstattung: Ryzen 7 5800X, 32 GB DDR4-3200
Ubuntu 22.04.3 LTS
GeForce RTX 3080 Ti
Wohnort: Saarbrücken
Kontaktdaten:

Re: Problem CheckFTPConnection(

Beitrag von NicTheQuick »

Dein Code gibt so was irres bei mir aus:
Erfolgreich verbunden. CheckFTPConnection(FTPVerbindung):1
0:tmp
1:tmptmp
2:tmptmptmp
3:tmptmptmptmp
4:tmptmptmptmptmp
5:tmptmptmptmptmptmp
6:tmptmptmptmptmptmptmp
7:tmptmptmptmptmptmptmptmp
8:tmptmptmptmptmptmptmptmptmp
9:tmptmptmptmptmptmptmptmptmptmp
10:tmptmptmptmptmptmptmptmptmptmptmp
11:tmptmptmptmptmptmptmptmptmptmptmptmp
12:tmptmptmptmptmptmptmptmptmptmptmptmptmp
13:tmptmptmptmptmptmptmptmptmptmptmptmptmptmp
14:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
15:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
16:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
17:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
18:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
19:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
20:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
21:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
22:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
23:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
24:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
25:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
26:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
27:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
28:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
29:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
30:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
31:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
32:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
33:tmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmptmp
Hab ihn dann abgebrochen.
Bild
Benutzeravatar
juergenkulow
Beiträge: 188
Registriert: 22.12.2016 12:49
Wohnort: :D_üsseldorf-Wersten

Re: Problem CheckFTPConnection(

Beitrag von juergenkulow »

Hallo NicTheQuick,

das Testprogramm gibt folgendes aus:
Wenn die Verbindung besteht wird jede Sekunde eine Stufe eines Tannenbaum ausgegeben mit dem ersten Verzeichniseintrags des FTP-Servers.
Wenn von CheckFTPConnection erkannt wird, dass die Verbindung nicht mehr besteht soll das Programm enden.
Wenn die Verbindung zeitweise unterbrochen ist, wird nichts ausgegeben für eine Weile.
Wenn die Verbindung entgültig unterbrochen ist, wird der Stamm des Tannenbaums ausgegeben.
Wenn die Verbindung nicht hergestellt werden konnte, gibt es eine Fehlermeldung und das Programmende.

Vorteil: Ich kann vom DSL-Modem aus sehen, ob das Programm noch Verbindung zum FTP-Server hat.
Die Laufzeit des Programms muss mindestens die Timeout-Zeit des Servers, 180 Sekunden, betragen.
Viel Freude mit dem Testprogramm.
Benutzeravatar
juergenkulow
Beiträge: 188
Registriert: 22.12.2016 12:49
Wohnort: :D_üsseldorf-Wersten

Re: Problem CheckFTPConnection(

Beitrag von juergenkulow »

Hallo PB-Universum,

anbei die aktuelle Version des Testprogramms mit der Bitte um Test auf Linux, MacOS und Veröffentlichung des Debug.
Die Gesamtlaufzeit des Test beträgt bei den derzeitigen Einstellungen etwa 230 Sekunden.
Mit Ende der Verbindung sollte CheckFTPConnection Null bzw. #False anzeigen, aber siehe unten.
Gruß und Danke.

Code: Alles auswählen

;Warum: CheckFTPConnection() und FtpCommand NOOP testen, finde Timeout heraus von ftp.free.fr
;erstellt mit PureBasic 5.51 x64 auf Windows 7 Home Premium SP1  
;Wie: Stelle eine FTP-Verbindung her und warte eine steigende Zeitdauer.
;  Teste jeweils mit einer Verzeichnisabfrage, die Funktion CheckFTPConnection und die Procedure neuerCheckFTPConnection 
;  ob die Verbindung noch besteht.
;  Gebe zum Schluß den Wert der Funktion CheckFTPConnection und neuerCheckFTPConnection aus. 
;offen 
;  Wie sendet man ein FTPCommand, wie NOOP, über eine mit OpenFTP geöffnete Verbingung? 
;  Linux MacOS Anpassung und Tests
EnableExplicit
Declare.i neuerCheckFTPConnection(FTPVerbindung)
Define WarteZeit=2
#TimeoOutMax=4000
InitNetwork()
Define                                Server$="ftp.free.fr", Benutzer$="anonymous", Passwort$=""
Define FTPVerbindung=OpenFTP(#PB_Any, Server$,               Benutzer$,             Passwort$)
If 0=FTPVerbindung : Debug "Konnte keine Verbindung zu "+Server$+" aufbauen. Dadurch ist kein Test möglich. - Programm wird beendet." : End: EndIf  
Debug  "Erfolgreich verbunden. CheckFTPConnection(FTPVerbindung):"+Str(CheckFTPConnection(FTPVerbindung))+" neuerCheckFTPConnection:"+Str(neuerCheckFTPConnection(FTPVerbindung)) 
If ExamineFTPDirectory(FTPVerbindung) 
  If NextFTPDirectoryEntry(FTPVerbindung) 
    Debug "Erstes Verzeichnis auf dem Server:"+FTPDirectoryEntryName(FTPVerbindung) ; Verbindung besteht
  Else 
    Debug "Kein Verzeichenniseintrag ist vorhanden. Dadurch ist kein Test möglich. - Programm wird beendet." : End 
  EndIf 
Else 
  Debug "Kein Verzeichennis ist auf dem Server vorhanden. Dadurch ist kein Test möglich. - Programm wird beendet." : End 
EndIf 
FinishFTPDirectory(FTPVerbindung) :  
Repeat
  Debug "Test mit der Wartezeit:"+WarteZeit+" Sekunden:"
  Delay(WarteZeit*1000)
  Debug "nach Sekunden:"+ Str(WarteZeit)+" CheckFTPConnection(FTPVerbindung):"+Str(CheckFTPConnection(FTPVerbindung)) +" neuerCheckFTPConnection:"+Str(neuerCheckFTPConnection(FTPVerbindung)) 
  If Len(Str(Wartezeit))=1 : Define s$=Str(Wartezeit+2) :  Else : s$=Str(Val(Left(Str(Wartezeit),1))+1)+Mid(Str(Wartezeit),2) : EndIf :  Wartezeit=Val(s$) ; Wartezeit erhöhen
  If ExamineFTPDirectory(FTPVerbindung) 
    If NextFTPDirectoryEntry(FTPVerbindung) 
      Define ErstesVerzeichnis$=FTPDirectoryEntryName(FTPVerbindung): 
      Debug "Die Verbindung ist vorhanden. - Erstes Serververzeichnis:"+ErstesVerzeichnis$ ;Verbindung bestht 
    Else 
      Debug "Die Verbindung ist weg. - Kein Verzeichenniseintrag." 
      ErstesVerzeichnis$="" 
    EndIf : FinishFTPDirectory(FTPVerbindung) 
  Else 
    Debug "Die Verbindung ist weg. - Kein Verzeichnis."
    ErstesVerzeichnis$="" 
  EndIf 
Until WarteZeit>#TimeoOutMax Or ""=ErstesVerzeichnis$ Or 0=CheckFTPConnection(FTPVerbindung)
Debug "CheckFTPConnection(FTPVerbindung):"+Str(CheckFTPConnection(FTPVerbindung)) +" neuerCheckFTPConnection:"+Str(neuerCheckFTPConnection(FTPVerbindung))

Procedure.i neuerCheckFTPConnection(FTPVerbindung)
  CompilerIf #PB_Compiler_OS=#PB_OS_Windows
    ;Define LFTPVerbindung.l=FTPVerbindung :  #FTP_TRANSFER_TYPE_ASCII=1 :    ProcedureReturn FtpCommand_(LFTPVerbindung, #False, #FTP_TRANSFER_TYPE_ASCII, "NOOP", 0, 0) ; Gibt immer Fehler zurück, weil FTPConnectHandle etwas anders als FTPVerbindung ist.
    If ExamineFTPDirectory(FTPVerbindung) : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #True : Else : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #False : EndIf
  CompilerElseIf #PB_Compiler_OS=#PB_OS_Linux
    If ExamineFTPDirectory(FTPVerbindung) : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #True : Else : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #False : EndIf
    ; ProcedureReturn CheckFTPConnection(FTPVerbindung)
  CompilerElseIf #PB_Compiler_OS=#PB_OS_MacOS
    If ExamineFTPDirectory(FTPVerbindung) : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #True : Else : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #False : EndIf
    ; ProcedureReturn CheckFTPConnection(FTPVerbindung)
  CompilerElse
    If ExamineFTPDirectory(FTPVerbindung) : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #True : Else : FinishFTPDirectory(FTPVerbindung) : ProcedureReturn #False : EndIf
    ; ProcedureReturn CheckFTPConnection(FTPVerbindung)
  CompilerEndIf 
EndProcedure

; Erfolgreich verbunden. CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Erstes Verzeichnis auf dem Server:tmp
; Test mit der Wartezeit:2 Sekunden:
; nach Sekunden:2 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:4 Sekunden:
; nach Sekunden:4 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:6 Sekunden:
; nach Sekunden:6 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:8 Sekunden:
; nach Sekunden:8 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:10 Sekunden:
; nach Sekunden:10 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:20 Sekunden:
; nach Sekunden:20 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:30 Sekunden:
; nach Sekunden:30 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:40 Sekunden:
; nach Sekunden:40 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:50 Sekunden:
; nach Sekunden:50 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:1
; Die Verbindung ist vorhanden. - Erstes Serververzeichnis:tmp
; Test mit der Wartezeit:60 Sekunden:
; nach Sekunden:60 CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:0
; Die Verbindung ist weg. - Kein Verzeichnis.
; CheckFTPConnection(FTPVerbindung):1 neuerCheckFTPConnection:0

Procedure.i NOOP Zeile 2868
Verbindung mit FTP-Server aufrecht halten? in PureBasic Forum 2006
schleicher
Beiträge: 214
Registriert: 30.03.2014 19:57
Computerausstattung: Purebasic 5.70
Wohnort: 18314 Löbnitz

Re: Problem CheckFTPConnection(

Beitrag von schleicher »

juergenkulow hat geschrieben:Hallo,

Tritt das Problem auch auf Linux und MacOS auf?
Kann ich leider nicht testen. Habe nur Windows.
Ich konnte das Problem anders lösen. Ich teste einfach, ob SetFTPDirectory( erfolgreich durch geführt werden kann. Wennn nicht , wird neu verbunden.

Code: Alles auswählen



Procedure CheckFTPConnect(id)
  Protected result, old_ftp_dirctory.s
  old_ftp_dirctory=GetGadgetText(Gadget_String_Pfad)
  If SetFTPDirectory(id, "/")
    result=1
    SetFTPDirectory(id, old_ftp_dirctory)
  Else
    result=0
  EndIf
  ProcedureReturn result
EndProcedure

Procedure checkftp()
  Protected Result
  If IsFTP(#ftp)<>0
    If CheckFTPConnect(#ftp)=0
      If ftp_connect()=#True
        Result=#True
      Else
        Result=#False
      EndIf
    ElseIf CheckFTPConnect(#ftp)<>0
      Result=#True
    EndIf
  Else
    If ftp_connect()=#True
      Result=#True
    Else
      Result=#False
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure

Purebasic 5.51
Antworten