shell 命令 exec 建立tcp连接与关闭连接

 shell 命令中要直接与java进程进行端口通信的话,可以使用exec 8>/dev/tcp/172.27.24.12/11111;

if [ "$?" != "0" ];then 
     echo "open $host $port fail!"; 
     exit 1; 
fi
echo "test">&8;
exec 8>&-
exit 0;

 注意:这里的关闭连接只是关闭了shell到java进程的一端。由于tcp是双工,所以java进程端也需要显示的关闭这个连接。

如果java进程使用的是NIO,则关闭socketchannel即可。

猜你喜欢

转载自shuofenglxy.iteye.com/blog/1163357