Dup'ing filehandles in Win32 
Author Message
 Dup'ing filehandles in Win32

This code works in Win32 (yes, there is a reason why I'm not using Socket.pm,
warn, strict or my):

#!perl

$sockaddr = 'S n a4 x8';
$bindthis = pack($sockaddr, 2, 9704, pack('l', chr(0).chr(0).chr(0).chr(0)));
socket(S, 2, 1, 6);
setsockopt(S, 65535, 4, 1);
bind(S, $bindthis) || die("$0: while binding port 9704:\n\"$!\"\n");
listen(S, 1024);

for (;;) {
        $addr=accept(NS,S);
        select(NS); $|++;
        $rin = $win = $ein = 0;
        vec($rin, fileno(NS), 1) = 1;
        vec($win, fileno(NS), 1) = 1;
        $ein = $rin | $win;
        $q = select($rout=$rin, $wout=$win, $eout=$ein, undef);
        while(<NS>) {
                chomp;
                last if (/quit/i);
                print NS (reverse(split(//, $_)));
                print NS "\r\n";
        }

Quote:
}

If I start this up, and telnet to 9704 on my Win32 box, I get

stockholm:/home/spectre/% telnet spectre 9704
Trying...
Connected to spectre.notyourproblem.net.
Escape character is '^]'.
yobaby
ybaboy
yo baby
ybab oy
quit
Connection closed.

This change, however, does not work:

for (;;) {
        $addr=accept(NS,S);
        select(NS); $|++;
        $rin = $win = $ein = 0;
        vec($rin, fileno(NS), 1) = 1;
        vec($win, fileno(NS), 1) = 1;
        $ein = $rin | $win;
        $q = select($rout=$rin, $wout=$win, $eout=$ein, undef);
        open(J, ">&NS"); select(J); $|++;
        open(K, "<&NS"); select(K); $|++;
        while(<K>) {
                chomp;
                last if (/quit/i);
                print J (reverse(split(//, $_)));
                print J "\r\n";
        }
        close(J); close(K);

Quote:
}

but it works just dandy on Unix. What's the difference? Is there any way
to dup filehandles properly in Win32?

What I eventually intend to do is dup STDIN and STDOUT off NS and hand
then to a child process using Win32::Process::Create(), sort of like a
miniature inetd. However, not even that seems to work right, even with
1 for the inherit handles option.

Any ideas?

--

  -- supporting the Commodore 64/128:   http://www.*-*-*.com/ ~spectre/cwi/ --
   head moderator comp.binaries.cbm * cbm special forces unit $ea31 (tincsf)
personal page http://www.*-*-*.com/ ~spectre/ * "when in doubt, take a pawn"



Sun, 11 Nov 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. 'dup'ing filehandles

2. Problem with dup'ing a FILEHANDLE

3. help: dup'ing/tee'ing

4. Small problem: dup'ing DATA and $.

5. INSERT'ing with Win32::ODBC

6. Can I dup a Filehandle object?

7. How to dup 'sort'

8. how to close() on filehandle without wait()ing?

9. 'tee'ing the stderr.

10. Newbie Question: 'kill'ing a script

11. re-'require'ing a file

12. 'cat'ing a GIF file?

 

 
Powered by phpBB® Forum Software