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 ="out.txt",w+)
Open3.popen2("./server.exe 4712") do |stdin, stdout, t|    
    stdout.each_line { |line| outfile.write(line) }

puts "Server now running"
system("./test_client localhost 4712")

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!



Add a Comment