The port is not in use before i run the script. It is in use when i
run it. However the problem is that only one iteration runs. I
expected all to run.

On Sun, May 15, 2016 at 1:52 AM, Jon E Price <jonelwoodpr...@gmail.com> wrote:
> Perhaps port 8989 is in use?
>
> Have you tried     >telnet 127.0 0.1 8989
>
> Can you connect?
>
> On May 14, 2016, at 3:56 PM, Unknown User <knowsuperunkn...@gmail.com>
> wrote:
>
>>I wrote this scrpt to fork off a few child processes, then the child
>>processes process some data, and send the data back to the parent
>>through a tcp socket.
>>This is not working as i expected it would. Why not? How can it be
>> corrected?
>>#!/usr/bin/perl -w
>>use strict;
>>use IO::Socket::INET;
>>print $$,"\n";
>>for my $num (1 .. 100) {
>>print "Working on: $num \n";
>>my $pid = fork;
>>if ($pid > 0) {
>># Parent
>>my $listen = IO::Socket::INET->new(
>>LocalAddr => "127.0.0.1",
>>LocalPort => "8989",
>>Listen => 1024,
>>ReuseAddr => 1,
>>Proto => 'tcp',
>>) or
>>die "Unable to listen: $!\n";
>>while(my $conn = $listen->accept()) {
>>warn "Got conn from ",$conn->peerhost,":",$conn->peerport,"\n";
>>my $line = $conn->read(1024);#do { local $/; <$conn>; };
>>if($line =~ /^(\d+):(\d+):.*/) {
>>print "Child with pid $1 from num $2 says $line\n";
>>}
>>else {
>>print "Invalid line : $line\n";
>>}
>>$conn->close();
>>}
>>exit(0);
>>}
>>elsif($pid == 0) {
>>my $sock =  IO::Socket::INET->new(
>>PeerAddr => "127.0.0.1:8989",
>>Proto => 'tcp',
>>);
>>unless($sock) {
>>warn "Unable to create sock to send data, child $num pid $$\n";
>>exit(1);
>>}
>>$sock->send( "$$:$num:, Child  exiting\n");
>>close($sock);
>>exit(0);
>>}
>>else {
>>warn "Unable to fork: $!\n";
>>}
>>}
>>====
>>]$ ./f.pl
>>6193
>>Working on: 1
>>Unable to listen: Address already in use
>>Got conn from 127.0.0.1:46546
>>Child with pid 6194 from num 1 says 6194:1:, Child  exiting
>>Use of uninitialized value $_ in print at ./f.pl line 21, <GEN21> line 1.
>>]$
>>--
>>To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>>For additional commands, e-mail: beginners-h...@perl.org
>>http://learn.perl.org/

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to