                 tftp-server get dup addr>> port>> tftp-servers get-global set-at
                 [
                     [ read-tftp-command t ]
-                    [ dup io-timeout? [ drop ] [ rethrow ] if f ] recover
+                    [ [ . flush ] with-global f ] recover
                 ] loop
             ] with-variable
         ] with-variable

        [ [ 1 + ] change-block block>> ] bi
        send-file-block
    ] [
        unknown-client
    ] if ;

: handle-send-file ( bytes -- )
    "\0" split harvest first2 [ utf8 decode ] bi@
    "netascii" sequence= utf8 binary ? 0 read-file boa
    tftp-client get clients get set-at
    0 handle-send-file-next ;

: read-tftp-command ( -- )
    tftp-server get receive tftp-client [

tftp-servers [ H{ } clone ] initialize
TUPLE: read-file path encoding block ;

: send-client ( bytes -- )
    tftp-client get tftp-server get send ;

: send-error ( message -- )
    [ TFTP-ERROR 1 ] dip 3array "SSa" pack-be send-client ;

: send-file-block ( bytes block -- )
    TFTP-DATA swap 2array "SS" pack-be B{ } prepend-as
    send-client ;

: read-file-block ( path n -- bytes )
    binary swap
    '[ _ 512 * seek-absolute seek-input 512 read ] with-file-reader ;

: handle-send-file-next ( block -- )
    drop
    tftp-client get clients get ?at [
