소켓을 사용하여 네트워크를 이용한 프로세스간 통신을 할수 있습니다. 아래의 예는, 127.0.0.1(localhost) 사이로 통신을 하는 예제입니다. 클라이언트로 Send 버튼을 누르면, 서버에 “Hello!” 라고 표시해줍니다.
클라이언트
set ip 127.0.0.1
set port 1234
set fd [socket -myaddr $ip $ip $port]
fconfigure $fd -buffering line
button .b -text Send -command {
puts $fd "Hello!"
}
pack .b
서버
set ip 127.0.0.1
set port 1234
pack [entry .e -textvariable var]
socket -server setup -myaddr $ip $port
proc setup {fd ip port} {
fileevent $fd readable "show $fd $ip $port"
fconfigure $fd -blocking 0
}
proc show {fd ip port} {
global var
if [eof $fd] {
close $fd
} else {
gets $fd line
set var $line
}
}
참고로 Windows만 지원하는 DDE 패키지를 사용하면 Tcl/Tk 어플리케이션 사이의 메세지 교환도 가능합니다.
