Skip to content

Instantly share code, notes, and snippets.

@ncimino
Created September 25, 2012 22:04
Show Gist options
  • Select an option

  • Save ncimino/3784754 to your computer and use it in GitHub Desktop.

Select an option

Save ncimino/3784754 to your computer and use it in GitHub Desktop.
Server Side - Tcl
#!/usr/bin/tclsh
set svcPort 8001
proc doService {sock msg} {
puts $sock "$msg"
}
proc svcHandler {sock} {
set l [gets $sock]
if {[eof $sock]} {
close $sock
} else {
doService $sock $l
}
}
proc read_stdin {wsock} {
global eventLoop
set l [gets stdin]
if {[eof stdin]} {
set eventLoop "done" ;# terminate the vwait (eventloop)
}
}
proc accept {sock addr port} {
fileevent $sock readable [list svcHandler $sock]
fconfigure $sock -buffering line -blocking 0
puts $sock "$addr:$port, you have connected to the server (message sent from server)."
set connect_time [clock format [clock seconds]]
puts $sock "\tServer Time: $connect_time"
puts "Accepted connection from $addr at $connect_time"
}
set esvrSock [socket -server accept $svcPort]
fileevent stdin readable [list read_stdin $esvrSock] ;# to enable exit on eof
vwait eventLoop ;# handle events till variable eventLoop is set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment