Reading from a named pipe

Hi all,

I need to read some text from a named pipe in ruby. The writing is done
in another (C++) program and I’d like to have Ruby display what is sent.

I’ve looked at the IO.pipe function, but there seems to be no way to get
a NAMED pipe. The two programs are running under Windows XP, btw.

Are named pipes even supported in Ruby? How can I read from one? Thanks
for your answers! :slight_smile:

Gc3bcnther Gruber wrote:

Hi all,

I need to read some text from a named pipe in ruby. The writing is done
in another (C++) program and I’d like to have Ruby display what is sent.

I’ve looked at the IO.pipe function, but there seems to be no way to get
a NAMED pipe.

Named pipes are opened and read or written as though they were files
(streams). Just open it by path and name.

The two programs are running under Windows XP, btw.

Are you asking how to create the named pipe? That is an operating system
function, and frankly, I don’t know if Windows supports them. The
operating
system must support the idea of a named pipe, and if it doesn’t, no
named
pipe.

Are named pipes even supported in Ruby?

As inputs and outputs, yes. This can be said of any language that opens
and
reads/writes streams. But the pipe itself must be supported and created
by
the OS.

How can I read from one?

Open it like a file.

Paul L. wrote:

How can I read from one?

Open it like a file.

Sounds simple, I’ll try that out. Thanks!

On Fri, 17 Nov 2006, [utf-8] Günther Gruber wrote:

Paul L. wrote:

How can I read from one?

Open it like a file.

Sounds simple, I’ll try that out. Thanks!

it’s a bit tough sometimes. the reason is that, by default, a process
will
hang when opening a named pipe unless there is a process on the other
end.
this is by design and one has to use non-blocking io to avoid it, eg

f = open ‘named.pipe’, File::RDWR|File::NONBLOCK

something to be aware of… here’s an example of some code which uses
named
pipes between a c program and ruby

http://codeforpeople.com/lib/ruby/acgi/acgi-0.1.0/

regards.

-a

On Nov 17, 2006, at 6:05 AM, Paul L. wrote:

named
pipe.

Windows also has named pipes, but they behave somewhat differently
than Unix named pipes. I only have experience with them in C and
Perl programming, but I found that you can’t do non-blocking reads on
a named pipe under Windows, and you can’t open, close and reopen the
same named pipe in a program. Basically, to use a named pipe for
accepting messages under Windows, you probably need to spawn a
separate thread to listen to it.

If you’re porting Unix software that uses named pipes to Windows, you
might be best off selecting a different IPC mechanism altogether,
such as sockets. In our porting efforts, we ended up writing a small
library that simulated Unix named pipes under Windows using shared
memory.

Tom

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs