While your socket is staying open the data is being buffered so not being flushed back to your client, so it won't see the response until the socket is closed (which won't happen unless you're telling it to (e.g. by sending a QUIT command that it understands) or the server hangs up on you)

In this example a web connection sends a "Connection: close" which gets the server to hangup - thus flushing the output.

#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $server = "remote.server.net"; my $port = 43; my $path = "/"; my $socket=IO::Socket::INET->new( PeerAddr => $server, PeerPort => $port, Proto=> 'tcp') || die "Can't connect to $server:$port: $!\n"; my $data = qq~GET $path HTTP/1.1 Host: $server Accept: */* User-Agent: My Perl Script Pragma: no-cache Cache-Control: no-cache Connection: close ~; print $socket "$data\r\n"; my $body = 0; while(<$socket>) { my $line = $_; if ($body eq 1) { print $line; } $body = 1 if ($line=~/Connection: close/i); } close($socket);
If you were connecting to an FTP server for example you would send "QUIT\n"

If you're reading the data in a while loop and then sending something which is hanging up the connection when you see what you want:

#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $remote_server = "ftp.server.net"; my $remote_port = 21; my $sock = IO::Socket::INET->new("$remote_server:$remote_port") || die + $!; my $data = "HELP"; my $string_to_compare = "^220 "; print $sock "$data\r\n"; my @output; $| = 1; while (<$sock>) { push(@output, $_); print $sock "QUIT\n" if /$string_to_compare/gi; } close($sock); my $output = join('', @output); sub true { print "Ain't dat da true! :o)\n"; # open(tmpl, "email.txt") || print $!; } sub false { print "Close, but no banana.\n"; } if ($output =~ /$string_to_compare/gi) { &true; } else { &false; }
it can sometimes make a difference if you put:
$| = 1;
Just before the while loop, which turns off buffering - thus auto-flushing the response from the server (see perlvar and look for $OUTPUT_AUTOFLUSH ) - but in your example the line
my @output = <$sock>;
is just going to sit there forever waiting for an EOF and the program will never get to the
$output = join('', @output);
to carry on with the rest of the logic.

In reply to Re: compare server response? by serf
in thread compare server response? by webshark

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.