zsh 通信脚本

server

 1 #!/bin/zsh
 2 #zsh TCP server script
 3 zmodload zsh/net/tcp
 4 #listening port
 5 ztcp -l 5150
 6 #This is a file describ mark $REPLY
 7 fd=$REPLY
 8 
 9 echo "Waiting for a client..."
10 #accept a new connect.
11 ztcp -a $fd
12 clientfd=$REPLY
13 echo "client connected"
14 
15 echo "welcome to my server" >& $clientfd
16 
17 while [ 1 ]
18 do
19 read line <& $clientfd
20 if [[ $line = "exit" ]]
21 then
22 break
23 else
24 echo Received: $line
25 echo $line >& $clientfd
26 fi
27 done
28 echo Client disconnected session
29 #Close fd and clientfd
30 ztcp -c $fd
31 ztcp -c $clientfd

client

 1 #!/bin/zsh
 2 #Zsh tcp client program
 3 zmodload zsh/net/tcp
 4 
 5 ztcp localhost 5150
 6 hostfd=$REPLY
 7 
 8 read line <& $hostfd
 9 
10 echo $line
11 while [ 1 ]
12 do 
13 echo -n "Enter text:"
14 read phrase
15 echo Sending $phrase to remote host...
16 echo "$phrase" >& $hostfd
17 #There is a small problem:if server is shut,client will continu run.Fortunately,after three request,the connect will close atuomatically.
18 if [[ $phrase = "exit" ]]
19 then 
20 break
21 fi
22 read line <& $hostfd
23 echo " received: $line"
24 done
25 ztcp -c $hostfd

 

posted @ 2015-06-05 23:32  little-snake  阅读(284)  评论(0编辑  收藏  举报