Skip to content

Instantly share code, notes, and snippets.

@johannish
Last active November 11, 2019 17:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannish/e28ce4cdcfa58c2f5fc3 to your computer and use it in GitHub Desktop.
Save johannish/e28ce4cdcfa58c2f5fc3 to your computer and use it in GitHub Desktop.
Simple socket example in Tcl
set chan [socket 127.0.0.1 9900]
while {1} {
puts -nonewline "Message? "
flush stdout
set msg [gets stdin]
puts $chan $msg
flush $chan
puts "server response: [gets $chan]"
}
proc accept {chan addr port} {
chan configure $chan -buffering line ;# automate flushing
puts "receiving from client"
while {1} {
set clientMsg [gets $chan]
puts "$addr:$port client $clientMsg"
puts $chan "received: $clientMsg"
#puts "sent $clientMsg"
}
}
socket -server accept 9900
vwait forever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment