User since: Aug 19, 2007 at 17:03 UTC (18 years ago)
Last here: Oct 15, 2016 at 07:50 UTC (9 years ago)
Experience: 44
Level:Novice (2)
Writeups: 13
Location:n/a
User's localtime: Dec 17, 2025 at 19:34 UTC
Scratchpad: None.
For this user:Search nodes


Posts by Kelicula
Socket programming Help in Seekers of Perl Wisdom
3 direct replies — Read more / Contribute
by Kelicula
on Apr 19, 2016 at 15:39

    I recently ran into a situation where I need to have many many raspberry pi's running all over the country and I need to "talk" to them from a central computer to tell them when to run a particular program I have pre-loaded on them. However the people that will be "hosting" them if you will are very very NON-computer savvy people, so I really can't have them configure their routers to set up VNC or the like connections, so I thought, I can solve this with "Sockets" right? :-) So what I want to do is have the pi's talk to my computer and say "I'm on and running, what do you want me to do?" and then I can send back to them what to do. Problem is I can't get the dang things to connect. :-( I have my router set to forward port 7777 to my computer and I have a no-ip service that hosts a name for my router.

    But every time the connection times out and won't connect . What am I doing wrong? Here is my code:

    Server Code

    use IO::Socket::INET; # auto-flush on socket $| = 1; # creating a listening socket my $socket = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '7777', Proto => 'tcp', Listen => 5, Reuse => 1 ); die "cannot create socket $!\n" unless $socket; print "server waiting for client connection on port 7777\n"; while(1) { # waiting for a new client connection my $client_socket = $socket->accept(); # get information about a newly connected client my $client_address = $client_socket->peerhost(); my $client_port = $client_socket->peerport(); print "connection from $client_address:$client_port\n"; # read up to 1024 characters from the connected client my $data = ""; $client_socket->recv($data, 1024); print "received data: $data\n"; # write response data to the connected client $data = "ok"; $client_socket->send($data); # notify client that response has been sent shutdown($client_socket, 1); } $socket->close();

    Client Code

    #!/usr/bin/perl use IO::Socket::INET; $| = 1; my $socket = new IO::Socket::INET ( PeerHost => 'theaddressofmycomputer.com', PeerPort => '7777', Proto => 'tcp', ); die "Cannot connect to the server $!\n" unless $socket; print "Connected to the server!\n"; my $req = "Hello World\n"; my $size = $socket->send($req); print "Sent data of length $size\n"; shutdown($socket, 1); my $response = ""; $socket->recv($response, 1024); print "Recieved response: $response\n"; $socket->close();
    ~~~~~~~~~ I'm unique, just like everybody else! ~~~~~~~~~
    UPDATE!!!

    OK, I got it working, thank you to everyone for all your help, it was the windows firewall. I simply had to create an inbound and outbound rule allowing communication on the port I want and it worked like a charm!

Sequential processing with fork. in Seekers of Perl Wisdom
4 direct replies — Read more / Contribute
by Kelicula
on Aug 04, 2015 at 15:19
    Greetings Perl Monks!

    I am NOT new to Perl but I am VERY new to "forking" or "threading". What I am trying to do is have a certain number of processes running continuously and when one finishes another one takes it's place... I almost had it this morning then after trying SO MANY different things, I can't even find the original code!! ARG.... But it's driving me crazy.. I know I am so close...

    So let's say I have 33 clients, and I want to process 10 of them at a time, I open one then the next, etc until I reach 10, then I want to wait until "any" one of those ten is finished, then start client 11, and so on so forth...

    Here's what I have so far...
    #!/usr/bin/perl use feature qw/say/; my $count = 1; CLIENT: for my $i (1..33){ if ( $count > 10 ) { say "waiting for open process"; while (1) { if ( wait() ){ $count = 1; redo CLIENT; } } } else{ # Stagger the initiating to help CPU sleep(5); } $count++ and next if( my $pid = fork() ); unless( $pid ){ say "Processing client $i process count $count"; # Emulate the time it would take to process.. sleep(60); exit; } }
    The out put I'm getting is:
    Processing client 1 process count 1 Processing client 2 process count 2 waiting for open process Processing client 3 process count 3 Processing client 4 process count 4 Processing client 5 process count 5 Processing client 6 process count 6 Processing client 7 process count 7 Processing client 8 process count 8 Processing client 9 process count 9 Processing client 10 process count 10 waiting for open process Processing client 11 process count 1 Processing client 12 process count 2 Processing client 13 process count 3 Processing client 14 process count 4 Processing client 15 process count 5 Processing client 16 process count 6 Processing client 17 process count 7 Processing client 18 process count 8 Processing client 19 process count 9 waiting for open process Processing client 20 process count 10 Processing client 21 process count 1 Processing client 22 process count 2 Processing client 23 process count 3 Processing client 24 process count 4 Processing client 25 process count 5 Processing client 26 process count 6 Processing client 27 process count 7 Processing client 28 process count 8 Processing client 29 process count 9 Processing client 30 process count 10 Processing client 31 process count 1 Processing client 32 process count 2 Processing client 33 process count 3

    What's going on here?

    What am I missing?

    I've also tried implementing it with Parallel:ForkManager and Thread::Queue ... Just seems that I'm missing something very simple yet elementary.

    Any help would be greatly appreciated!! All I doing now is opening 10 clients and waiting an hour to open the next set using:

     system( "pathtoperlfile", "args");

    In a loop, but it's wasting SO MUCH time, I think I could increase my productivity by 2-3 fold if I could maintain a constant number of processes...

    I humbly pray for the wisdom of the Monks...

    UPDATE: I'm running this on Windows Server 2012 with Dwimperl. I've been informed that fork is "emulated" on windows and that server may kill long running processes that are just "waiting". Any idea how to handle this? Kelicula
    ~~~~~~~~~ I'm unique, just like everybody else! ~~~~~~~~~
substitution in Seekers of Perl Wisdom
5 direct replies — Read more / Contribute
by Kelicula
on Aug 24, 2007 at 21:07
    I am trying to open a file, and search for a srting. Once found I want to substitute it for something else and acually change the file. ie) File named "stuff.txt" contains: Code: ( text ) Hello I am Kelly Hello I am David Hello I am mark I want to find David and change it to Josh. This is how I am trying to do it, but it doesn't work.
    open (FIL, "+<stuff.txt") or die "$!"; foreach $line (<FIL>){ if($line =~ m/David/){ $line =~ s/David/Josh/; } print $line; }
    What am I doing wrong? I tried opening the file with ">>" and "+>>", to no avail. ???