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

I'm going over your code for clues that may help me. In the meantime, here is the code I am working on. Server.pl works fine as is, but if I try to put the code inside a while loop (I have tried both inside the socket open/close calls and outside) it quits working.

edit: I changed my server.pl a bit based on what I saw in your example. This does not work. The while loop goes through the first iteration fine, and hangs up the second time through with a (error|warning) message of "Use of uninitialized value in substitution (s///) at /clocal/mptsgrp/user/mptsuser/scripts/guipartner.pl line 24, <GEN1> line 2."
The print statements I included for debug show that $incoming matched /.+/ even after it was set to "" on the first iteration, but it did fail the $command[0] eq "check" test. The killer, though, is that the while loop stopped there. "Hi" was never printed a third time. :-(
#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $incoming; my $check; my @check; my @command; my $new_sock; my $sock = new IO::Socket::INET ( LocalPort => '8888', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; $new_sock = $sock->accept(); while ($incoming = <$new_sock>){ print "hi"; if ($incoming =~ m/.+/){ print $incoming; @command = split / /,$incoming; $command[1] =~ s/\W+//g; $incoming = ""; if ($command[0] eq "check"){ $check = @check = `ps -ef |grep $command[1]`; print "$check\n@check\n"; if ($check >= 12) { print $new_sock "up"; } else { print $new_sock "down"; } }else{ print"did not work\n"; } } } close $sock;


Likewise, the client works, so long as I don't duplicate the code to send a second request to the server.
#!/usr/bin/perl use strict; use warnings; use IO::Socket; use Fcntl; my $input; my $sock; my $cs2; my $mqpcx; my $mpts; my $EOL = "\015\012"; my $BLANK = $EOL x 2; my $command; open OUT, "> testpage.html" or die "unable to write testpage"; ### OUT is temporary until I get cgi working. open FILE1, "< 1.html" or die; while (<FILE1>){print OUT} close FILE1; $sock = new IO::Socket::INET ( PeerAddr => '53.230.116.97', PeerPort => '8888', Proto => 'tcp', ); if ($sock){ print "Connection established\n"; $command = "check cs2" . $BLANK; print $sock $command; while ($cs2 = <$sock>){ last if ($cs2 =~ m/.+/); } if ($cs2 eq "up") { print OUT "<img src=\"./cs2up.gif\">\n"; $mpts++; print"so far, so good\n"; } elsif ($cs2 eq "down") { print OUT "<img src=\"./cs2down.gif\">\n"; } else { print "error getting data from other box"; } }else{ print OUT "<img src=\"./cs2down.gif\">\n"; print "no connection to other box"; } close($sock); close OUT; print "goodbye\n";

--
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)

Replies are listed 'Best First'.
Re^4: two-way socket works once only?
by ikegami (Patriarch) on Nov 23, 2004 at 19:51 UTC

    Well, if I look at the server, it goes:

    • Get a bunch of text from the client.
    • Run ps, etc
    • Send response to client.
    • Close client.

    Shouldn't that be:

    • While the client is still connected,
      • Get a command from the client.
      • Run ps, etc
      • Send response to client.
      You beat me to my edit... Please recheck my previous post, just updated.

      --
      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)

        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);