#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Select; # Server_rebuild.pl my( $EC, $pid); my $SERVER = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '50000', Proto => 'tcp', Listen => 3, Reuse => 1, ); die "Could not create socket: $!\n" unless $SERVER; while(1) { # loop for the parent # create a connection to incoming EC accept($EC, $SERVER); next if $pid = fork; # parent die "fork: $!" unless defined $pid; # failure #=================== Now, it's all about the child processes ==================# # the socket of the parent is no use to child close($SERVER); # my $new_sock = $sock->accept(); my( $select, @ok_to_read, @ok_to_write, $buf, $handle2 ); #$handle1, $select = IO::Select->new(); $select->add($EC); while ($EC){ @ok_to_read = $select->can_read(1); @ok_to_write = $select->can_write(1); foreach my $handle1 (@ok_to_read) { my $buf = ''; my $rv = sysread($handle1, $buf, 64*1024, length($buf)); # Handle error # Handle eof while ($buf =~ s/(.*\n)//) { print $1; # Full line at a time } } foreach $handle2 (@ok_to_write){ print $handle2 "Hello from the SERVER new_sock\n"; print $EC "Hello from the SERVER new_sock\n"; print "Trying to write to the client\n"; } #sleep (1); } } #$loop for the parent