大网数据广告活动
服务器运维问答

BSD nc (netcat) does not terminate on EOF

· 52CE 我爱测

Host A: tar cf - stuff | dd | nc -N -l 12987 Host B: nc a.example.com 12987 | dd | tar tf - On host A dd prints its summary after tar completes. Thus it is clear, that tar closes the pipe/file -> EOF . 165040+0 records in 165040+…

问题原文

原帖链接 · georg · CC BY-SA 3.0

Host A:

tar cf -  stuff | dd | nc  -N -l 12987

Host B:

nc a.example.com 12987 | dd | tar tf - 

On host A dd prints its summary after tar completes. Thus it is clear, that tar closes the pipe/file -> EOF.

165040+0 records in 165040+0 records out 84500480 bytes transferred in 25.464802 secs (3318325 bytes/sec)

On both hosts nc happily sits there without exiting. nc(1) :

   -N      shutdown(2) the network socket after EOF on the input.  Some
           servers require this to finish their work.

Thus on host A nc should have seen EOF, closed the damn socket and on host B nc should have seen the TCP connection terminate and should have closed stdout (stdin of dd/tar).

How do I tell nc to close stdout / terminate on host B and terminate on host A.

nc bug?

-D (debug) does nothing. nc can't even tell its version number... sigh

Both hosts are FreeBSD 10.3-RELEASE-p4, IPv4 only.

已采纳解答

原帖链接 · ens · CC BY-SA 4.0

I, too, was puzzled by netcat's behavior, so I dug into the code. Here's the whole story:

nc servers (nc -l) and clients only exit after the mutual connection was closed. That is, if each of the parties sent a FIN packet to the other party.

A server always sends a FIN packet after receiving a FIN packet from the client. (Unless the server already sent a FIN packet.)

A client sends a FIN packet either:

  • after EOF on stdin, when run with argument -N
  • after EOF on stdin, when the server already sent a FIN packet

With option -d stdin is ignored and nc behaves as if it encountered EOF on stdin.

Option -N always implies sending FIN after encountering EOF on stdin.

Ways to exit the nc processes after exchanging data:

  1. Georg's answer

     server$ echo hello | nc -l -N 2000
     client$ nc -d localhost 2000
    

    After sending hello, the server encounters EOF on stdin an sends FIN because of -N.

    The client receives the message and, due to -d, sees EOF on stdin and sends FIN, because the server already sent FIN.

    The connection is closed, both the client and the server exit.

  2. Client initiates the close

     server$ echo hello | nc -l 2000
     client$ nc -dN localhost 2000
    

    The server keeps the connection open after EOF on stdin.

    The client sees EOF on stdin and sends FIN, because of -N.

    The server sends FIN after receiving the client's FIN.

    The connection is closed, both the client and the server exit.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-06-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。