in reply to Re^5: two-way socket works once only?
in thread two-way socket works once only?

I didn't know where to begin, so I pretty much rewrote them X_X It was basically overly complex. If you wonder why I made a certain change, feel free to ask.

I did find it odd that "12" is hardcoded in the server (as opposed to being looked up by $process_name, or specified by the client), but "cs2" isn't.

server.pl

#!/usr/bin/perl # die commands probably need to be replaced with something else. use strict; use warnings; use IO::Socket; my $EOL = "\015\012"; my $listen_sock = new IO::Socket::INET ( LocalAddr => '127.0.0.1', ### LocalPort => '8888', Proto => 'tcp', Listen => 1, Reuse => 1, ) or die("Could not create socket: $!\n"); my $client_sock = $listen_sock->accept() or die("Error accecpting connnection: $!\n"); { local $/ = $EOL; my $incoming; while (defined($incoming = <$client_sock>)) { chomp($incoming); die("Received nothing.\n") unless length $incoming; my $command; ($command, $incoming) = split /\s+/, $incoming, 2; if ($command eq "check") { my $process_name = $incoming; die("check: Bad process name.\n") unless $process_name =~ /^\ +w+$/; # my $num_processes = grep { /$process/ } `ps -ef`; my $num_processes = int(rand()*24); ### print $client_sock ($num_processes >= 12 ? "up" : "down"), $E +OL; next; } die("Bad command $command.\n"); } } close($client_sock); close($listen_sock);

client.pl

#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $EOL = "\015\012"; my $mpts; ### Until I get cgi working. open OUT, "> testpage.html" or die("Unable to open output file: $!\n"); select(OUT); if (0) ### { local *HEADER; open HEADER, "< 1.html" or die("Unable to open header file: $!\n"); print while (<HEADER>); close HEADER; } my $sock = new IO::Socket::INET( # PeerAddr => '53.230.116.97', PeerAddr => '127.0.0.1', ### PeerPort => '8888', Proto => 'tcp', ) or die("Error creating connnection: $!\n"); for (1..20) { my $command = "check cs2"; print $sock $command, $EOL; local $/ = $EOL; my $reply = <$sock>; die("Lost connection to server.\n") unless defined $reply; chomp($reply); if ($reply eq "up") { print STDERR "up\n"; print "<img src=\"./cs2up.gif\">\n"; $mpts++; } elsif ($reply eq "down") { print STDERR "down\n"; print "<img src=\"./cs2down.gif\">\n"; } else { die("Bad reply $reply\n"); } sleep(1); } close($sock); close(OUT);

Replies are listed 'Best First'.
Re^7: two-way socket works once only?
by wolfger (Deacon) on Nov 29, 2004 at 14:54 UTC
    Okay, this definitely works. Thanks for the fix. Now I need to understand a few things.
    In server.pl:
    why do you have LocalPort => '127.0.0.1',  ###? Shouldn't that be LocalAddress?
    why while (defined($incoming = <$client_sock>)) { instead of while ($incoming = <$client_sock>){?
    In client.pl:
    I don't understand the use of if (0). Is that just to disable that section of code? That is the only thing I can think of...

    Again, thanks for the help!

    --
    Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
    (Buddha)

      Yes, LocalPort => '127.0.0.1' is a typo for LocalAddr => '127.0.0.1'. The latter means only accept connections from 127.0.0.1. (Actaully, it means listen for connections whose destination address is 127.0.0.1, which should mean the same thing.) I used that for testing.

      while (defined($incoming = <$client_sock>)) can indeed be simplified to while ($incoming = <$client_sock>). Old habbit. It's not needed here, and it's not needed when doing while (defined($line = <$fh>)), where I picked up the habbit.

      Yes, if (0) is just an easy way to comment out a section. I didn't have a header file.

        Okay, thanks for the explanations. Seems that I basically was hanging things up with my client, and thinking that the problem was with my server. It's all sorted out now, and works like a charm. I cleaned up my code a bit, too... threw some things into subroutines, and quit calling a long list of "my" at the beginning (which even I thought was ugly). I'll provide the "finished" versions if anybody cares to see how it wound up.

        --
        Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0