Reading from a named pipe

Discussion in 'Ruby' started by Günther Gruber, Nov 17, 2006.

  1. 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! :)

    --
    Posted via http://www.ruby-forum.com/.
    Günther Gruber, Nov 17, 2006
    #1
    1. Advertising

  2. Paul Lutus wrote:
    >> How can I read from one?

    >
    > Open it like a file.


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

    --
    Posted via http://www.ruby-forum.com/.
    Günther Gruber, Nov 17, 2006
    #2
    1. Advertising

  3. Günther Gruber

    Guest

    On Fri, 17 Nov 2006, [utf-8] G=C3=BCnther Gruber wrote:

    > Paul Lutus 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 =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
    , Nov 17, 2006
    #3
  4. Günther Gruber

    Tom Pollard Guest

    On Nov 17, 2006, at 6:05 AM, Paul Lutus wrote:
    > 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.


    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
    Tom Pollard, Nov 20, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruintje Beer

    named pipe in java

    Bruintje Beer, Sep 27, 2005, in forum: Java
    Replies:
    2
    Views:
    9,105
    Roedy Green
    Sep 27, 2005
  2. rajnish

    c++ named pipe example

    rajnish, Oct 22, 2004, in forum: C++
    Replies:
    5
    Views:
    19,419
    John Harrison
    Oct 25, 2004
  3. lee, wonsun
    Replies:
    1
    Views:
    486
    Jack Klein
    Nov 2, 2004
  4. Miguel P
    Replies:
    3
    Views:
    554
    Miguel P
    Sep 12, 2009
  5. Replies:
    1
    Views:
    222
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page