2

私はシェルスクリプトにまったく慣れていません。私の問題は、ソケット接続を有効にする方法がよくわからないことです。私が書いたスクリプトが本来の動作をしているかどうかはわかりません。

私がやろうとしているのは、140 個のソケットを作成し、これらのソケットにメッセージを送信すると信じており、これらのソケット接続を維持したいということです。

for (( i=0; i<140; i++))
do
echo "Message" > /dev/tcp/187.98.06.01/4599
done

このメッセージを送信するサーバーがあり、サーバー側でクライアント接続が確立されているというログメッセージも取得しますが、ソケット接続の数を確認すると、数は0です。ソケット接続が閉じられているためです。

ソケット接続を維持する必要があります。誰かが上記のコードの提案または修正を提供できますか?

4

1 に答える 1

2

I use GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu).

This is not terribly safe and not terribly portable, but you should be able to write:

# Open 140 TCP connections to port 4599 on 187.98.06.01, using
# file-descriptors 60 through 199:
for (( i = 0 ; i < 140 ; i++ )) ; do
    fd=$((i + 60))
    eval exec "$fd<>/dev/tcp/187.98.06.01/4599"
    echo Message >&$fd
done

# ... do whatever you want with these 140 connections ...

# Close the connections:
for (( i = 0 ; i < 140 ; i++ )) ; do
    fd=$((i + 60))
    eval exec "$fd<&-" "$fd>&-"
done

(The reason it's not portable is that it depends on Bash features that aren't shared by all shells. The reason it's not safe is that the shell uses file-descriptors for its own internal stuff, e.g. opening scripts and searching directories. Descriptors 0 through 9 — STDIN, STDOUT, STDERR, and seven others — are intended for your use, but descriptors 10 and higher risk conflicting with what the shell is doing. In Bash 4.x there's a feature that lets you provide a variable-name to the redirection and let Bash decide what file-descriptor to use, but Bash 3.x doesn't have that.)

于 2012-11-16T14:05:23.717 に答える