Neatware Header
Home   Products   Solutions   Partners   Store   Company   

Network Examples

  • Echo via Socket

    Below example starts a server and waits client to send a string and echo back to client.

    Server

    callback function must have three parameters: socket handle, address, and port.

    proc callback {sock addr port} {
    	fconfigure $sock -translation lf -buffering line
    	fileevent $sock readable [list echo $sock]
    }
    

    echo fuction reads from socket and puts the line back.

    proc echo {sock} {
    	global var
    	if {[eof $sock] || [catch {gets $sock line}]} {
    		close $sock; set var done
    	} else {
    		puts $sock "$line"; puts "show echo: $line"
    	}
    }
    

    init socket and wait done.

    set port 2800
    set sh [socket -server callback $port]
    puts "socket is ready"
    vwait var; close $sh
    

    Client

    open a client socket in localhost.

    set host localhost; set port 2800
    set s [socket $host $port]
    fconfigure $s -buffering line; puts $s $host
    


Google
  Web Neatware.com