24.8 TCP Examples

Here is a trivial example using a remote calculator.

To create a calculator server on port 7337 (see the dc manual page for quite how infuriating the underlying command is):

tcp_proxy 7337 dc

To connect to this from the same host with a session also named ‘dc’:

tcp_open localhost 7337 dc

To send a command to the remote session and wait a short while for output (assuming dc is the current session):

tcp_command 2 4 + p

To close the session:

tcp_close

The tcp_proxy needs to be killed to be stopped. Note this will not usually kill any connections which have already been accepted, and also that the port is not immediately available for reuse.

The following chunk of code puts a list of sessions into an xterm header, with the current session followed by a star.

print -n "\033]2;TCP:" ${(k)tcp_by_name:/$TCP_SESS/$TCP_SESS\*} "\a"