Created
May 18, 2011 10:09
-
-
Save knu/978321 to your computer and use it in GitHub Desktop.
simple netcat written in zsh etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have a bug where the same key for
fds
will be set twice if both fd0
and fd$so
become readable in the same select call.You can fix it by using the
-A
argument tozselect
instead of-a
.