sockets: Serverantwort komplett auslesen



  • Hallo:
    woher weiss der Client, dass er die Antwort vom Server vollständig erhalten hat?

    bissl Pseudo-Code:

    // zum Server verbinden
    connect();
    
    // Antwort auslesen
    response = recv();
    
    // mit der Serverantwort irgendwas anstellen.
    // Antwort muss aber komplett ausgelesen sein
    doSomething(response);
    

    Konkret geht's darum, die Statusmeldungen bei einer FTP-Verbindung auszulesen.
    (Stress macht hauptsächlich der Serverwelcome nach dem connect(), wenn dieser zu lange ist)

    Geholfen hat ein Sleep() zwischen connect() und recv().
    Aber wie lange soll das Programm schlafen?
    1000 ms?
    Wenn der Server aber mal einen schlechten Tag hat, und 1500ms braucht?

    Danke schon im Voraus.
    Martin



  • hi all,

    schau mal das hier an:

    int bytest_recv;
    
    while( (bytes_recv = recv(sock, buf, sizeof(buf), 0)) > 0 )
    {
       write ( 1, buf, bytes_recv);
    }
    
    if( bytes_recv == -1 )
       printf("Error\n");
    

    also zeichenweise empfangen bis alles empfangen ist, also solange bytes_recv>0 👍



  • Danke für die Antwort,
    aber recv() liefert 0 zurück, wenn die Verbindung getrennt worden ist,
    was aber nicht der Fall ist.



  • Wird die Antwort beim FTP-Protokoll nicht mit irgendwas terminiert (\n\r\n\r oder so?). Dann musst du noch solange recv aufrufen, bis du die Terminierung erhalten hast.



  • D@niel $chumann schrieb:

    Wird die Antwort beim FTP-Protokoll nicht mit irgendwas terminiert (\n\r\n\r oder so?). Dann musst du noch solange recv aufrufen, bis du die Terminierung erhalten hast.

    Hab zur Sicherheit grad nochmal Ethereal angeworfen.
    Jede Zeile wird mit \r\n terminiert.
    Der Serverwelcome hat allerdings mehrere Zeilen.

    Mir ist aber grade aufgefallen, dass nur die letzte Zeile der Antwort
    ein Leerzeichen nach dem Statuscode hat:

    220----------------------------------------------------------------------
    220-Welcome on ********************** running Novell NetWare 6.x
    220-Please note that passwords are transferred in clear text.
    220----------------------------------------------------------------------
    220 Service Ready for new User
    

    Da werd ich mal in dieser Richtung weitersuchen...

    thx
    Martin



  • Die Richtung ist gut, ich hab eben mal einen kurzen Blick ins RFC 959 geworfen:

    A reply is defined to contain the 3-digit code, followed by Space <SP>, followed by one line of text (where some maximum line length has been specified), and terminated by the Telnet end-of-line code. There will be cases however, where the text is longer than a single line. In these cases the complete text must be bracketed so the User-process knows when it may stop reading the reply (i.e. stop processing input on the control connection) and go do other things. This requires a special format on the first line to indicate that more than one line is coming, and another on the last line to designate it as the last. At least one of these must contain the appropriate reply code to indicate the state of the transaction. To satisfy all factions, it was decided that both the first and last line codes should be the same.

    Thus the format for multi-line replies is that the first line will begin with the exact required reply code, followed immediately by a Hyphen, "-" (also known as Minus), followed by text. The last line will begin with the same code, followed immediately by Space <SP>, optionally some text, and the Telnet end-of-line code.

    ( http://www.freesoft.org/CIE/RFC/959/24.htm )


Anmelden zum Antworten