我创建谁接受所有的连接,并执行进来的数据具有命令行TCP服务器,但是当我发出“退出”到的TCPSocket,过程和插座不密妥
# main.cr
require "socket"
PORT = 2022
def handle_connection(socket)
Process.run("/bin/sh", input: socket, output: socket, error: socket)
end
server = TCPServer.new(PORT)
loop do
if socket = server.accept?
spawn handle_connection(socket)
else
break
end
end
例如,下面的代码工作精细,发送“退出”到STDIN后,外壳退出“过程结束”被printedand程序密切
channel = Channel(Nil).new
spawn do
Process.run("/bin/sh", input: STDIN, output: STDOUT, error: STDERR)
puts "process ending"
channel.send(nil)
end
channel.receive
对于debuggin目的,我已经测试此代码太多,但“结束进程”从来没有打印,直到我手动关闭TCP套接字
# main.cr
require "socket"
PORT = 2022
def handle_connection(socket)
Process.run("/bin/sh", input: socket, output: socket, error: socket)
puts "process ending"
end
server = TCPServer.new(PORT)
loop do
if socket = server.accept?
spawn handle_connection(socket)
else
break
end
end
当我运行main.cr, nc localhost 2022
和发送“退出”我预计接近插槽正确,但他不,当我发送更多的指令中的程序产生错误
Unhandled exception in spawn: Error writing file: Broken pipe (Errno)
from /usr/lib/crystal/crystal/system/unix/file_descriptor.cr:79:13 in 'unbuffered_write'
from /usr/lib/crystal/io/buffered.cr:122:14 in 'write'
from /usr/lib/crystal/io.cr:1130:7 in 'copy'
from /usr/lib/crystal/process.cr:413:7 in 'copy_io'
from /usr/lib/crystal/process.cr:409:11 in 'copy_io:close_dst'
from /usr/lib/crystal/process.cr:298:17 in '->'
from /usr/lib/crystal/fiber.cr:255:3 in 'run'
from /usr/lib/crystal/fiber.cr:47:34 in '->'
from ???