In order to solve a programming assignment (send a “TEST” to an UDP service and readout the result [beeing "success"]) i looked into socket programming with the BASH command shell.
Here is the code:
# configurationHOST="127.0.0.1"PORT="1337"# define functionssocksend (){ SENDME="$1" echo "sending: $SENDME" echo -ne "$SENDME" >&5 &}sockread (){ LENGTH="$1" RETURN=`dd bs=$1 count=1 <&5 2> /dev/null`}echo "trying to open socket"# try to connectif ! exec 5<> /dev/udp/$HOST/$PORT; then echo "`basename $0`: unable to connect to $HOST:$PORT" exit 1fiecho "socket is open"# send requestsocksend "TEST"# read 7 bytes for "success"sockread 7echo "RETURN: $RETURN"
The procedure is fairly easy:
- create a filedescriptor (using number 5 because 0,1,2 are for system stdin/stdout/stderr, so i am on the safe side) and link it to the special device /dev/udp (/dev/tcp for tcp connections). - read/write to the fdAttention: your bash must be compiled with this feature, otherwise there wont be any /dev/udp or /dev/tcp devices.
My post should answer the question posted here:
Also, i stumbled across those pages which where quite helpful for getting started: