bash socket

#!/bin/bash

#config
HOST="127.0.0.1"
PORT="3000"

#functions
#send message
sendMessage()
{
message="$1"
echo "sending: $message"
echo -ne "$message" >&6 &
}
#read message
readMessage()
{
length="$1"
returnMessage=`dd bs=$1 count=1 <&6` 
}


echo "try to open socket"
if ! exec 6<>/dev/tcp/$HOST/$PORT
then
echo "open socked failed"
exit 1
fi
echo "socket is open"

#send message
sendMessage "hello,this is nana"

#read message
readMessage 8
echo "returnMessage: $returnMessage"

你可能感兴趣的:(shell,socket,bash)