Skip to content

Instantly share code, notes, and snippets.

@knu
Created May 18, 2011 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knu/978321 to your computer and use it in GitHub Desktop.
Save knu/978321 to your computer and use it in GitHub Desktop.
simple netcat written in zsh etc.
var net = require('net');
var host = process.argv[2];
var port = parseInt(process.argv[3]);
var client = net.createConnection(port, host);
client.on('data', function (chunk) { process.stdout.write(chunk) } );
process.stdin.resume();
process.stdin.on('data', function (chunk) { client.write(chunk) } );
#!/usr/bin/env zsh
zmodload zsh/net/tcp
zmodload zsh/zselect
zmodload zsh/system
ztcp "$@"
so=$REPLY
while :; do
typeset -A fds
zselect -t 30 -a fds -r 0 -r $so
for fd in ${fds}; do
case $fd in
0) sysread -i 0 -o $so -t 0 ;;
*) sysread -i $so -o 1 -t 0 ;;
esac
case $? in
1|2|3) exit ;;
esac
done
done
@eatnumber1
Copy link

You have a bug where the same key for fds will be set twice if both fd 0 and fd $so become readable in the same select call.

You can fix it by using the -A argument to zselect instead of -a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment