unix_sockets(3tcl)

unix_sockets3tcl

Communicate using unix domain sockets

Usage

unix_sockets::listen path accept_handler
unix_sockets::connect path


    


    

Overview

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.

unix_sockets::listen path accept_handler

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.

unix_sockets::connect path

Connect as a client to the unix domain socket path. The channel handle for the new socket is returned.

Examples

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

Keywords

socket, channel, unix, ipc

From tcl-unix-sockets 0.5-3 in Debian 13 (Trixie). Parsed by carta from the page's man source.