# sock2.tcl # Verbindung aufbauen # zeilenweise Übertragung proc Connect {} { global host port sock set sock [socket $host $port] fconfigure $sock -blocking 0 -buffering line fileevent $sock readable Read focus .in .in configure -background #ffffff } # Verbindung beenden proc Disconnect {} { global sock close $sock set sock {} .in configure -background #000000 return } # Eventroutine für empfangene Daten proc Read {} { global sock # Verbindungsabruch -> Socket schließen if [eof $sock] { Disconnect return } # keine neuen Daten -> return if {[gets $sock line] < 0} { return } # Daten ausgeben .out insert end "$line\n" .out see end update idletasks } # Daten abschicken, sobald Enter gedrückt proc Send {} { global sock if {$sock == {}} return set line [.in get "insert linestart" "insert lineend"] puts $sock $line flush $sock } # Main # 1. GUI generieren label .lh -text "Host:" entry .eh -textvariable host label .lp -text "Port:" entry .ep -textvariable port button .bc -text "Connect" -underline 0 -command Connect button .bd -text "Disconnect" -underline 0 -command Disconnect text .in -background #000000 text .out grid .lh -row 0 -column 0 grid .eh -row 0 -column 1 grid .lp -row 1 -column 0 grid .ep -row 1 -column 1 grid .bc -row 2 -column 0 grid .bd -row 2 -column 1 grid .in -row 3 -column 0 -columnspan 2 grid .out -row 4 -column 0 -columnspan 2 # Shortcuts bind . {Connect} bind . {Disconnect} # Behandlung Return bei Eingabe bind .in Send focus .eh # Vorgabewerte set host localhost set port 80