unix_sockets(3tcl) unix_sockets(3tcl)


unix_sockets - Communicate using unix domain sockets

unix_sockets::listen path accept_handler
unix_sockets::connect path

Unix domain sockets are a filesystem based IPC mechanism similar to IP sockets, except that they can only be used by processes on the same system. This package provides a wrapper to use them from Tcl scripts.

Open a listening socket with the path path. If path already exists, it is unlinked first. You must have permission to create path in it's parent directory. When a client connects to the listening socket, accept_handler is invoked with the channel handle of the new client connection appended.
Connect as a client to the unix domain socket path. The channel handle for the new socket is returned.

A simple server that echos back to the client the first line it sends, then closes the socket:

proc readable {con} {
    set msg [gets $con]
    puts $con $msg
    close $con
}
proc accept {con} {
    chan event $con readable [list readable $con]
}
set listen   [unix_sockets::listen /tmp/example.socket accept]
vwait ::forever

A client that opens a socket, writes a line to it, waits for a response and exits:

proc readable {con} {
    set msg [gets $con]
    puts "got response: ($msg)"
    set ::done 1
}
set con     [unix_sockets::connect /tmp/example.socket]
puts $con "hello, world"
flush $con
chan event $con readable [list readable $con]
vwait ::done

close(n), chan(n), socket(n), read(n), puts(n), eof(n), fblocked(n), flush(n)

socket, channel, unix, ipc

0.1 Tcl-Extensions