Created
September 25, 2012 22:04
-
-
Save ncimino/3784754 to your computer and use it in GitHub Desktop.
Server Side - Tcl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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