castaway,

from your code I was able to go on, thanks for this nice push. I packed the Server & Handle stuff in one package having in mind that I might be able to listen for new clients within a spawned kid and pass the updated valid client-handles to its parent.

But this failed and want to ask now:

is there a way to pass those handels or the updated class var.: @CL (which contains all client-handles) to another process?

package SoServ; #: one server for all clients use IO::Socket::INET; use IO::Select; my @CL = (); #: array of sock.Handles to Clients my $S; #: Socket-Server my $H; #: for IO::Select sub new { #: new Socket-Server my $me = {}; $me->{'a'} = shift; #: loc.Adddress address $me->{'p'} = shift; #: port to connect $S = IO::Socket::INET->new( LocalAddr => $me->{'a'}, LocalPort => $me->{'p'}, Proto => 'tcp', Listen => 1, Reuse => 1, Timeout => 60 ); die "Can't create SoServ ($!)\n\n" unless $S; $H = IO::Select->new(); $H->add($S); return bless $me; } #: wait for new connections sub waitNewConn { my $me = shift; #: either endless (1) or just 2 clients (0): my $withKids = shift; while (1) { # endless Loop if with Kids! # wait timeout-sec and then try again: my $newCL = $S->accept() || next; print $newCL "Hi, guy, nice meeting you\n"; $H->add($newCL); #update @CL instead of:push @CL,$newCL; @CL = $H->can_write(1); # no kid: return after 2 conn. return @CL if (@CL == 2 && !$withKids); } # while (1) } # sub sub clients { return @CL } ####### end packages ############# package main; my $addr = 'localhost'; # hard coded for now my $port = 2345; # dito my $Serv = SoServ::new( $addr,$port ); # start Server # Array of Handles to Clients derived from SoServ->@CL my @Clients = (); my $withKids = 1; # want kids or everyth. in 1 process? if ($withKids) { spawn($Serv); # kid is waiting for new Clients } else { # no kid, waiting here for new (2) Clients @Clients = $Serv->waitNewConn(0); } my $line = ''; my $f = '/home/cas/Data/sp1Min/spAdj.dta'; my $n = 9; # just 9 lines for now open(SP, "< $f") or die "can't open SP: $f: $!"; # while (defined($line = <SP>)) { print $line; # control @Clients = $Serv->clients() if ($withKids); for (@Clients) { print $_ $line; } last if (!$n--); } close(SP); exit; sub spawn { my $S = shift; my $pid; if (!defined($pid = fork)) { print "cannot fork: $!"; return; } elsif ($pid) { print " I'm the Server-Parent: pid:$pid \$\$:$$\n"; return; # I'm the parent } $S->waitNewConn(1); # yes with Kids, of course => (1) exit; } # end sub spawn

In reply to Re: Re: Socket: Server shall send same $line to several clients by Anonymous Monk
in thread Socket: Server shall send same $line to several clients 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.