#!/bin/zsh
#Zsh tcp client program
zmodload zsh/net/tcp
ztcp localhost 5150
hostfd=$REPLY
read line <& $hostfd
echo $line
while [ 1 ]
do
echo -n "Enter text:"
read phrase
echo Sending $phrase to remote host...
echo "$phrase" >& $hostfd
#There is a small problem:if server is shut,client will continu run.Fortunately,after three request,the connect will close atuomatically.
if [[ $phrase = "exit" ]]
then
break
fi
read line <& $hostfd
echo " received: $line"
done
ztcp -c $hostfd
>>>
#!/bin/zsh
#zsh TCP server script
zmodload zsh/net/tcp
#listening port
ztcp -l 5150
#This is a file describ mark $REPLY
fd=$REPLY
echo "Waiting for a client..."
#accept a new connect.
ztcp -a $fd
clientfd=$REPLY
echo "client connected"
echo "welcome to my server" >& $clientfd
while [ 1 ]
do
read line <& $clientfd
if [[ $line = "exit" ]]
then
break
else
echo Received: $line
echo $line >& $clientfd
fi
done
echo Client disconnected session
#Close fd and clientfd
ztcp -c $fd
ztcp -c $clientfd