带有 nc 的双向 socat 功能

Ben*_*Ben 3 netcat socat

这个问题与https://superuser.com/questions/270698/how-is-it-called-when-listeningconnecting-two-sockets-and-exchanged-data-betw有某种关系

在测试设置中,我打开了三个终端窗口

Term1: "nc -l 55545"
Term2: "nc -l 55546"
Term3: "socat tcp:localhost:55545 tcp:localhost:55546"
Run Code Online (Sandbox Code Playgroud)

Term1 的输入现在出现在 Term2 中,Term2 的输入出现在 Term1 中。

这是所需的行为。如何仅使用 nc 来实现此行为?

当我跑进去

Term3: "nc localhost 55545 | nc localhost 55546"
Run Code Online (Sandbox Code Playgroud)

然后 Term1 的输入出现在 Term2 中,但 Term2 的输入出现在 Term3 中。我怎样才能使管道双向?如果可能,没有临时文件。

lar*_*sks 13

这直接来自netcat 上维基百科页面。在 Term3 中,您将运行:

mkfifo backpipe
nc localhost 55545 0<backpipe | nc localhost 55546 1>backpipe
Run Code Online (Sandbox Code Playgroud)

这几乎完全符合您的要求。它使用 FIFO 将左侧的输出返回到右侧。严格来说,它不是临时文件——FIFO 是两个进程之间的命名管道。