Question: how to do (repeated) bidirectional communication with another process via stdin/stdout?


(Carsten Gronski) #1

Thanks a lot for your response. Using IO.readline() instead of IO.each() solved the problem !
Fortunatly I can identify the last line of a response String, which is returned by the system I ’ m communicating with, so I can do readline() until the last line is reached and write again after having collected and parsed the previous response.

– Carsten

···

ruby-talk@ruby-lang.org schrieb am 16.06.02 02:52:02:

ke = IO.popen(“cat”,“r+”)
loginCall = “blablabla\n” # cat needs \n at the end before it echoes back the line
sysInfoCall = “fofofofo\n”

if (ke != nil)
ke.syswrite(loginCall)

#flush() is not needed if you use syswrite, as #flush only flushes

ruby’s internal buffering and ruby does not perform any buffering

with syswrite and sysread

#ke.flush()

#each("\n") blocks until it finds \n or EOF.

#ke.each("\n") { |line| puts(line) }

if you don’t know the number of lines to be read, then you have to use

IO#select so your program does not block unnecessarily.

alternatively if you now the number of lines to be read, just invoke

#readline that many times.

puts ke.readline

ke.syswrite(sysInfoCall)
#ke.flush() # ditto, syswrite does not require flush

ke.close_write

the #each here works because the condition \n or EOF is true.

ke.each("\n") { |line| puts(line) }

ke.close
end


FreeMail in der Premiumversion! Mit mehr Speicher, mehr Leistung, mehr
Erlebnis und mehr Pramie. Jetzt unter http://club.web.de/?mc=021105