Skip to content

Instantly share code, notes, and snippets.

@suxue
Created March 15, 2014 13:50
Show Gist options
  • Save suxue/9567630 to your computer and use it in GitHub Desktop.
Save suxue/9567630 to your computer and use it in GitHub Desktop.
#!/bin/ksh
if [[ -z "$1" ]] || (( $1 <= 1024 )); then
echo 'usage: tcpserver port'
exit 1
fi
typeset port=$1
typeset pipe=`mktemp -u`
mkfifo $pipe
trap "rm -f $pipe" INT
function server {
echo "server listening on $port" >&2
cat $pipe | nc -l localhost "$port" | while read line; do
printf "%s\n" "$line"
done
}
function loopback {
cat - > $pipe
}
server | loopback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment