Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Win98 socket woes

by Dinosaur (Beadle)
on Apr 08, 2002 at 13:22 UTC ( [id://157430]=perlquestion: print w/replies, xml ) Need Help??

Dinosaur has asked for the wisdom of the Perl Monks concerning the following question:

Sort of in the same vein as Bi directional Socket question:

I can't get a pair of processes to communicate by socket, under Win98 with ActiveState perl 5.6.1.

I've read the Perl Cookbook ch. 17 advice, and also the relevant sections of Mastering perl/Tk (since that's what I'm trying to do, move some heavy lifting to a separate process so as not to slow down GUI response). What I'm doing, I think, is right in line with their recommendations. I've tried various permutations of parameters, with no success.

Code is below. When I run it, I get the following messages:

child started IO::Socket::INET=GLOB(0x204aeb8) child read failed: Invalid argument at E:\Perls\sock_hop.pl line 63. parent started IO::Socket::INET=GLOB(0x176541c) parent sending parent sending parent sending parent write failed: Invalid argument

... of which the failed read in the child seems to be the smoking gun. "Invalid argument" presumably refers to the file handle -- but what's its problem?

If anyone here can take a minute to look at the attached code snippet, I'd be grateful.

--Dinosaur

use strict; use warnings; use IO::Socket; sub process; sub parent; sub child; use vars qw[$PARENT $CHILD]; process; exit; sub process { $PARENT = IO::Socket::INET->new( Type => SOCK_STREAM, Proto => 'tcp', LocalHost => 'localhost', LocalPort => 9969, Listen => 5, ); die "parent socket failed: $!\n" unless defined $PARENT; $CHILD = IO::Socket::INET->new( Type => SOCK_STREAM, Proto => 'tcp', PeerAddr => 'localhost', PeerPort => 9969, ); die "child socket failed: $!\n" unless defined $CHILD; if (my $pid = fork()) { close $PARENT; parent; } else { die "fork failed: $!\n" unless defined $pid; close $CHILD; child; } } sub parent { print STDERR "parent started $CHILD\n"; while (1) { print STDERR "parent sending\n"; print $CHILD "Hello from parent\n" or die "parent write failed: +$!\n"; sleep 2; } } sub child { print STDERR "child started $PARENT\n"; while (1) { my $line = readline ($PARENT); die "child read failed: $!" unless defined $line; print STDERR $line; } }

Replies are listed 'Best First'.
Re: Win98 socket woes
by perlplexer (Hermit) on Apr 08, 2002 at 14:00 UTC
    You can't read/write from/to the socket that you are listening on ($PARENT in your case). You need to call accept() first. See this section of perlipc for details.

    --perlplexer

      But I'm not listening on $PARENT, ...

      AAAARRRGGGHHHH!!! It's BACKWARDS!

      I knew I'd get confused if I didn't think up better names for the two filehandles.

      I'll have to wait until I get home to try it, but that should do the trick. Thanks, 'plexer.

      --Dinosaur

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://157430]
Approved by vek
Front-paged by rbc
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-24 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found