Reading from a named pipe

G

Günther Gruber

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! :)
 
A

ara.t.howard

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 =3D open 'named.pipe', File::RDWR|File::NONBLOCK

something to be aware of... here's an example of some code which uses name=
d
pipes between a c program and ruby

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

regards.

-a
--=20
my religion is very simple. my religion is kindness. -- the dalai lama
 
T

Tom Pollard

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


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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top