afa4c85cca | ||
---|---|---|
.gitignore | ||
README.md | ||
bind-input | ||
bind-output | ||
master | ||
pool | ||
read | ||
write |
README.md
bash socket utility
native calls
1. bind-input
Binding an input creates a listening socket on a local port and binds the socket data to a local accessible buffer (accessible with the native read
call).
bind-input $id $port;
Arguments:
$id
is a unique arbitrary name you must give to the listening binding$port
is the number of the local port to listen to (between 1024 and 49151)
Execution:
- The
bind-input
execution will last until the socket is created, then it will move it to a background process and print its PID. - In order to close properly the
bind-input
, you must kill its PID with one of the 3 following signals: SIGINT, SIGHUP, SIGTERM. Note that you must not use SIGKILL because it won't trigger the "proper closing" routine.
Exemple:
Let's say that you want to listen to port 9999, then read every second. You want to ignore empty messages (if nothing received), and end the program when you receive the message "END".
#/bin/bash
# 1. Create the listening socket + store PID in /tmp
bind-input input1 9999 > /tmp/get_bg_pid;
# 2. Store PID in a variable
bound_pid="`cat /tmp/get_bg_pid`";
# 3. Now the socket is listening, you can read every 1 sec
while true; do
# 3.1. Read received data from port 9999
message="`read input1`";
# 3.2. Ignore empty messages
[[ -z "$message" ]] && sleep 1 && continue;
# 3.3. Exit while() loop if received "END"
[[ "$message" = "END" ]] && break;
# 3.4. Wait 1 sec
sleep 1;
done;
# 4. Kill 'bind-input' with signal SIGINT, SIGHUP or SIGTERM
kill -INT $bound_pid;
2. bind-output
Binding an output creates a local accessible buffer (accessible with the native write
call) bound to a socket connecting to a certain host and port.
bind-output $id $host $port;
Arguments:
$id
is a unique arbitrary name you must give to the output binding$host
is the hostname (IP addr. or resolvable) of the machine to connect to.$port
is the number of the remote port to connect to (between 1024 and 49151)
Execution:
- The
bind-output
execution will last until the socket is created, then it will move it to a background process and print its PID. - In order to close properly the
bind-output
, you must kill its PID with one of the 3 following signals: SIGINT, SIGHUP, SIGTERM. Note that you must not use SIGKILL because it won't trigger the "proper closing" routine.
Exemple:
Let's say that you want to connect to port 192.168.0.2 at port 9999, wait 2 seconds, send a message, wait 1 second and send the "END" message.
#/bin/bash
# 1. Create the connecting socket + store PID in /tmp
bind-output output1 192.168.0.2 9999 > /tmp/get_bg_pid;
# 2. Store PID in a variable
bound_pid="`cat /tmp/get_bg_pid`";
# 3. Wait for sending first message
sleep 2;
write output1 "some message\nwith\tescapable\e[1mcharaters\e[0m";
# 4. Wait for sending "END" message
sleep 1;
write output1 "END";
# 5. Kill 'bind-output' with signal SIGINT, SIGHUP or SIGTERM
kill -INT $bound_pid;