gpt4 book ai didi

Connect stdin and stdout to a socket(将标准输入和标准输出连接到套接字)

转载 作者:bug小助手 更新时间:2023-10-24 20:41:53 25 4
gpt4 key购买 nike



Imagine a microserver that is listening on some port. It receives ASCII newline-terminated requests and sends ASCII newline-terminated responses. Simple. Easy.

设想一个正在监听某个端口的微服务器。它接收以ASCII换行符终止的请求并发送以ASCII换行符终止的响应。很简单。很简单。


Now imagine a simple script that prints a request on stdout and listens for a response on stdin:

现在设想一个简单的脚本,它在stdout上打印一个请求,并监听stdin上的响应:


#!/bin/bash
while true; do
echo "A request" # send a request to the microserver via stdout
read # block until a response is received from microserver via stdin
echo "Server response is" $REPLY
done

[Of course this script could be written in C or Python or SNOBOL or whatnot, but the point is that it prints something on stdout and waits for a reply on stdin.]

[当然,此脚本可以用C、Python、SNOBOL或其他语言编写,但关键是它在stdout上打印一些内容,并等待stdin上的回复。]


Given that, how would I connect stdout and stdin of this script to the socket / port # that the microserver is listening on? Assume that I'm running on a Linux-like system. I see hints that mkfifo or nc might be parts of the answer, but I've not been able to connect the dots.

鉴于此,我将如何将该脚本的stdout和stdin连接到微服务器正在侦听的套接字/端口号?假设我在一个类似Linux的系统上运行。我看到了一些迹象,mkfio或nc可能是答案的一部分,但我一直无法将这些点联系起来。


更多回答

Under bash, you could use exec {requestfd}<>/dev/tcp/host/2345; echo >&$requestfd "$therequest";read -u $requestfd theanswer, but under posix shell, you have to use mkfifo and binary netcat: nc!! I'll maybe post an answer lather...

在bash下,您可以使用exec{questfd}<>/dev/tcp/host/2345;ho>&$questfd“$therquest”;读取-u$questfd答案,但在POSIX外壳下,您必须使用mkfio和二进制Netcat:nc!!我可能会发一封回信给你。

优秀答案推荐

Bash-specific: You can use /dev/tcp/ as a magic name prefix that creates and connects a socket.

特定于Bash的:您可以使用/dev/tcp/作为创建和连接套接字的神奇名称前缀。


So:

所以:


exec 3<>/dev/tcp/servicehost/12345
echo >&3 "A request"
read <&3 response
exec 3>&-
echo "Server response is $response"


To connect the stdout and stdin of your script to a socket/port that the microserver is listening on, you can use a combination of nc (netcat) and named pipes (FIFOs). Here's how you can do it:

要将脚本的stdout和stdin连接到微服务器正在侦听的套接字/端口,您可以使用NC(Netcat)和命名管道(FIFO)的组合。以下是你如何做到这一点:



  1. Create a named pipe (FIFO) for both input (to send requests) and output (to receive responses). You can use mkfifo for this purpose.


    mkfifo request_fifo
    mkfifo response_fifo


  2. Modify your script to read from the request FIFO and write to the response FIFO:


    #!/bin/bash
    while true; do
    echo "A request" > request_fifo # send a request to the microserver via the request FIFO
    read < response_fifo # block until a response is received from microserver via the response FIFO
    echo "Server response is" $REPLY
    done



Use nc to connect the microserver's socket to the request and response FIFOs. Run nc in the background to handle the communication between your script and the microserver.

使用NC将微服务器的套接字连接到请求和响应FIFO。在后台运行nc以处理脚本和微服务器之间的通信。


nc -l -p <port> < <(cat response_fifo) | while read line; do
echo "$line" > request_fifo
done > response_fifo &

Replace <port> with the actual port number that your microserver is listening on.

替换为您的微服务器正在侦听的实际端口号。


Now, when you run your script, it will send requests to the microserver via the request FIFO and receive responses via the response FIFO. The nc command acts as a bridge between the script and the microserver, forwarding data between the named pipes and the socket.

现在,当您运行脚本时,它将通过请求FIFO向微服务器发送请求,并通过响应FIFO接收响应。NC命令充当脚本和微服务器之间的桥梁,在命名管道和套接字之间转发数据。


Make sure to replace <port> with the actual port number used by your microserver, and ensure that the script and the nc command are running in the background as appropriate for your use case.

确保将 替换为您的微服务器使用的实际端口号,并确保脚本和nc命令根据您的用例在后台运行。


更多回答

Under bash you may prefer to use a variable to store auto assigned file descriptor as: exec {request}<>/dev/tcp/host/2345;echo >&$request ...

在bash下,您可能更喜欢使用变量将自动分配的文件描述符存储为:exec{请求}<>/dev/tcp/host/2345;ECHO>&$REQUEST...

This answer looks ... generated.

这个答案看起来..。已生成。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com