TheMagician has asked for the wisdom of the Perl Monks concerning the following question:
### tcp server
The client part is giving me headaches.use DBI; use IO::Socket::INET; ### flush after every write $|= 1; ### infinite loop while(1) { my($socket, $client_socket); my($peeraddress, $peerport); my($row, $data); my @processes= ('one', 'two', 'three', 'four', 'five', 'six', 'seven +', 'eight', 'nine', 'ten'); # creating object interface of IO::Socket::INET modules which intern +ally does # socket creation, binding and listening at the specified port addre +ss. $socket = new IO::Socket::INET ( LocalHost=> '127.0.0.1', LocalPort=> '5000', Proto=> 'tcp', Listen=> 5, Reuse=> 1 ) or die "ERROR in Socket creation: $!\n"; print "I am waiting clients to connect on port 5000.\n"; while($row= shift(@processes)) { $client_socket = $socket->accept(); $peeraddress = $client_socket->peerhost(); print "Sending $row to $peeraddress:$peerport)... "; # write to the newly accepted client. print $client_socket "$row\n"; # read from the newly accepted client. $data = <$client_socket>; chomp($data); print "got $data from client.\n"; $client_socket->close(); } $socket->close(); }
### tcp client
I am not able to adapt the sfork to accomplish this task. Any help? All the best, TheMagician (Paolo F.)use strict; use warnings 'all'; sub sfork($&) { my($max, $code)= @_; foreach my $c (1..$max) { wait unless $c<=$max; die "Cannot fork: $!\n" unless defined(my $pid= fork); exit $code->($c) unless $pid; } 1 until -1 == wait; } sfork 2, sub { sub getFromProducer { use IO::Socket::INET; my($socket, $data); $socket= new IO::Socket::INET ( PeerHost=> '127.0.0.1', PeerPort=> '5000', Proto=> 'tcp' ) or die "ERROR in Socket creation: $!\n"; $socket->autoflush(1); $data= <$socket>; chomp($data); $socket->close(); return $data; } while(my $data= &getFromProducer) { print "($$) Got $data from producer.\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: fork always 'n' processes.
by tybalt89 (Monsignor) on Mar 21, 2018 at 00:17 UTC | |
by Anonymous Monk on Mar 21, 2018 at 09:21 UTC | |
|
Re: fork always 'n' processes.
by TheMagician (Initiate) on Mar 21, 2018 at 16:40 UTC | |
|
Re: fork always 'n' processes.
by Anonymous Monk on Mar 20, 2018 at 15:25 UTC | |
by Anonymous Monk on Mar 20, 2018 at 16:48 UTC | |
by hippo (Archbishop) on Mar 20, 2018 at 16:55 UTC | |
by Anonymous Monk on Mar 20, 2018 at 17:06 UTC |