For some reason this code hangs for few seconds when accessing images from urls. I sped up html requests by watching for other signs that I was at the end of the data stream. What am I missing? Is there a way to speed up the socket closure after it is done reading?
#!perl use strict; use Socket; my $url="http://www.cpan.org/misc/jpg/cpan.jpg"; my $host="www.cpan.org"; $|=1; my $start=times; my ( $iaddr, $paddr, $proto ); $iaddr = inet_aton( $host ); $paddr = sockaddr_in( 80, $iaddr ); $proto = getprotobyname( 'tcp' ); unless( socket( SOCK, PF_INET, SOCK_STREAM, $proto ) ) {die "ERROR Dud +e: getUrl socket: $!";} unless( connect( SOCK, $paddr ) ) {die "getUrl connect: $!\n";} my @head=( "GET $url HTTP/1.1", "User-Agent: Mozilla/4.78 [en] (X11; U; Safemode Linux i386)", "Pragma: no-cache", "Host: $host", "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, imag +e/png, */*", "Accept-Language: en" ); push(@head,"",""); #Build Header and print to socket my $header=join("\015\012",@head); print "sending request\n$header\n\n"; select SOCK; $| = 1; binmode SOCK; print SOCK $header; my $body=''; while( <SOCK> ) { my $data=$_; $data=~s/[\r\n\t]+$//s; $data=~s/^[\r\n\t]+//s; last if $data=~/^0$/s; my $len=length($data); #print STDOUT "len:$len\n"; $body .= $data; last if $data=~/\<\/html\>$/is; if($data=~/\<\/body\>$/is){ $body .= qq|</html>|; last; } #print STDOUT "$data\n"; } unless( close( SOCK ) ) { return ( "getUrl close: $!" ); } select STDOUT; close SOCK; my $end=times; my $diff=$end=$start; print "Took $diff to access page\n";

In reply to using Socket to get urls hangs for several seconds. by slloyd

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.