Hello, I have a big problem that I can't solve. I need to write something kinda telnet server in perl (not exactly telnet, but it should support 2-way communication with a child it started). I can't succeed redirecting input/output of a child to the internet socket - the child reports Bad file descriptor on every write to stdout). I've wrote a sample child in Perl to diagnose a problem (save to testfile-slave.pl):
#!/usr/bin/perl use strict; select(STDIN);$| = 1; select(STDOUT);$| = 1; print STDERR "starting interaction\n"; $! = ''; print "GET /\n\n"; print STDERR "printerr is $! " . fileno(STDIN) . ", " . fileno(STDOUT) + . "\n"; print STDERR "req sent\n"; $! = ''; while (<STDIN>) { my $v = $_; print STDERR "readerr is $!\n"; print STDERR $v; } print STDERR "readerr is $!\n"; print STDERR "done interaction\n"; ----------- I tried several versions of servers, and all behave the same: #------version1 - works fine on linux #!/usr/bin/perl use strict; use IO::Socket; my $conn = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "192.168.0.12", #change to any hos +tname PeerPort => 80, ) || die("can't connect"); open(STDIN,"<&" . $conn->fileno); open(STDOUT,">&" . $conn->fileno); system("$^X testfile-slave.pl"); print STDERR "done\n"; -------------------- #---------version2 - windows-specific #!/usr/bin/perl #use strict; use IO::Socket; my $conn = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "192.168.0.12", #use address of any web server here PeerPort => 80) || die("can't connect"); binmode($conn); open(STDIN,"<&" . $conn->fileno) || print STDERR "can't redir stdin\n" +; open(STDOUT,">&" . $conn->fileno) || print STDERR "can't redir stdout\ +n"; my $file = "testfile-slave.pl"; use Win32::Process; use Win32; my ($ProcessObj,$exitcode); if (Win32::Process::Create($ProcessObj, "$^X","$^X $file", 1, NORMAL_PRIORITY_CLASS,".")) { $ProcessObj->Wait(500000); $ProcessObj->Kill($exitcode); } else { print STDERR "failed executing $file\n"; }; print STDERR "done\n"; --------------------
It appears to be some problems with Windows (e.g. possibly total inability to redirect IO from sockets) - does anybody knows about this? May be using IPC::open3 and IO::Select will help? Tested on Win98SE with ActiveState perl 5.8.0. Thanks in advance. (PS: I've searched entire google, and didn't find anything useful).

BazB changed pre tags to code tags.


In reply to Redirecting input and output of child process to inet socket fails on Win32 by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.