Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过socat bash Web服务器通过http发送图像_Bash_Http_Webserver_Netcat_Socat - Fatal编程技术网

通过socat bash Web服务器通过http发送图像

通过socat bash Web服务器通过http发送图像,bash,http,webserver,netcat,socat,Bash,Http,Webserver,Netcat,Socat,我正在尝试使用socat在bash中编写一个web服务器。我在处理图像请求时遇到问题。我正在打开socat侦听连接,如下所示: socat -T 30 -d -d TCP-L:$LISTENIP,reuseaddr,fork,crlf SYSTEM:"$0 \"docroot=$DOCROOT\"" 我用下面的代码提供图像,其中$1是docroot,$2是图像文件名 function serve_png { if [ -e $1$2 ] then SIZE=`stat -c '

我正在尝试使用socat在bash中编写一个web服务器。我在处理图像请求时遇到问题。我正在打开socat侦听连接,如下所示:

socat -T 30 -d -d TCP-L:$LISTENIP,reuseaddr,fork,crlf SYSTEM:"$0 \"docroot=$DOCROOT\""
我用下面的代码提供图像,其中$1是docroot,$2是图像文件名

function serve_png {
  if [ -e $1$2 ]
  then
    SIZE=`stat -c '%s' $1$2`
    echo -ne "HTTP/1.1 200 OK\nContent-type: image/png\nContent-length: $SIZE\n\n"
    cat $1$2
  else
    echo -ne "HTTP/1.1 404 Not Found\nContent-type: text/html\n\n404 - Not found\n"
  fi
}
该图像无法在firefox中显示,因为它“包含错误”

2014/01/25 08:00:41 socat[11551] N listening on AF=2 0.0.0.0:8080
2014/01/25 08:00:45 socat[11551] N accepting connection from AF=2 $MYIP:55765 on AF=2 $SERVERIP:8080
2014/01/25 08:00:45 socat[11552] N forking off child, using socket for reading and writing
2014/01/25 08:00:45 socat[11551] N forked off child process 11552
2014/01/25 08:00:45 socat[11551] N listening on AF=2 0.0.0.0:8080
2014/01/25 08:00:45 socat[11552] N forked off child process 11553
2014/01/25 08:00:45 socat[11552] N forked off child process 11553
2014/01/25 08:00:45 socat[11552] N starting data transfer loop with FDs [4,4] and [3,3]
2014/01/25 08:00:45 socat[11552] W read(3, 0x8e2e388, 8192): Connection reset by peer
2014/01/25 08:00:45 socat[11552] N socket 2 to socket 1 is in error
2014/01/25 08:00:45 socat[11552] N socket 2 (fd 3) is at EOF
2014/01/25 08:00:45 socat[11552] N socket 1 (fd 4) is at EOF
2014/01/25 08:00:45 socat[11552] N socket 2 (fd 3) is at EOF
2014/01/25 08:00:45 socat[11552] N exiting with status 0

我见过使用netcat的类似脚本,但我无法使用socat使其正常工作。我希望继续使用socat,因为它具有分叉和处理多个连接的能力。如果您有任何见解,我们将不胜感激。

Steffen Ullrich在socat命令中省略了crlf标志。这导致socat自动将Cariage返回/换行插入到流中(因此导致文件损坏)。省略该选项后,一切正常。

你真是个怪胎!这样:)您是否在文本/十六进制编辑器中打开了该图像?该图像尚未在任何类型的编辑器服务器端打开。为了测试的目的,我在启动服务器之前将其删除。我的意思是,从socat服务器下载文件后,您是否检查了该文件?打开“/home/spikec/test.png”失败:读取“/home/spikec/test.png”时出错。文件损坏?当我在文本编辑器中查看它们时,似乎有一些不应该出现的换行符。在多次编辑后,无法在此评论中按需要换行:(请尝试忽略crlf选项,并在echo语句中使用\r\n而不是\n。虽然我找不到crlf选项的任何文档,但我假设它与文档中的crnl相同,在这种情况下,它会将PNG中的所有0x0a字节更改为0x0d0a,从而损坏图像。