Ruby Scripting- How to run a client and server program simultaneously and collect server output

问题内容:

all. I’m trying to create a ruby script that initializes and runs a server program in the background (usually always running), and then runs a short client program once, then kills the server. Both the server and client are made in C++, and the server runs on localhost, port 4712. The port is a command line argument to the server. The server must be initialized with a “./init_serv” executable, and once it starts running, it must be fed “user1 password1” through cin. I want the output of server collected into a file, “out.txt”.
This is what I have so far:

require 'open3'

userinput = "user1 password1"
outfile = File.new("out.txt",w+)
system("./init_server")
Open3.popen2("./server.exe 4712") do |stdin, stdout, t|    
    stdin.puts(userinput)
    stdin.close
    stdout.each_line { |line| outfile.write(line) }
    end
end

puts "Server now running"
system("./test_client localhost 4712")
Process.kill("SIGINT", t.pid)

I thought that the “popen2” call would make a new thread to run the server program, but when I run this, the program blocks (the server doesn’t exit unless manually killed) and never prints the “Server now running” statement. Any suggestions on how to solve this? I’m fairly new to scripting in general, so other advice is appreciated too.

Thanks in advance!

问题评论:

原文地址:

https://stackoverflow.com/questions/47756683/ruby-scripting-how-to-run-a-client-and-server-program-simultaneously-and-collec

添加评论

友情链接:蝴蝶教程