Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Socket Server Speed

by wileykt (Acolyte)
on Jul 01, 2002 at 13:37 UTC ( [id://178543]=perlquestion: print w/replies, xml ) Need Help??

wileykt has asked for the wisdom of the Perl Monks concerning the following question:

I'm running a socket server program that receives files from a perl client. When the files are received, they are printed out to a directory. The programs function fine, the question I have, is does anyone have suggestions on how to speed up the receipt of the files? It's not as fast as I'd like it to be. Below find the main portion of the socket server code.

Thanks - Keith
$server = IO::Socket::INET->new( Listen => 5, LocalPort => 1111, Proto => 'tcp', Reuse => 1, Type => SOCK_STREAM ) or die "Can't create server socket: $!"; while ($client = $server->accept()) { $receivefile = $receivepath . $timestamp . ".ord"; $client->autoflush(1); open FILE, $receivefile or die "Can't open: $!"; while (<$client>) { if (substr($_,0,4) ne $trnh && substr($_,0,4) ne $ordh && substr($ +_,0,4) ne $ordl && substr($_,0,4) ne $endt && substr($_,0,4) ne $trlr +) { $sizereceived = substr($_,0,5); print FILE substr($_,5); } else { print FILE $_; } $client->autoflush(1); sleep(1); } #End While Receiving from Client close FILE; check_size(); #If the filesize sent doesn't match #the filesize received, send an error #email. !!!Do not indent this if statement, #the sendmail won't work!!! if ($filesize != $sizereceived) { move_badfiles(); $badpath = substr($receivefile,41); #Start SENDMAIL open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq"); print SENDMAIL <<"EOF"; From: marc To: someone\@someplace.com Subject: Distn Planning File Error! File $badpath has not be transmitted properly. Check the badinput dir +ectory. EOF close(SENDMAIL); } else { move_files(); }#End else $timestamp = strftime("%m%d%Y%H%M%S", localtime()); $receivetime = strftime("%m/%d/%Y %H:%M:%S", localtime()); print "Successfully received $receivefile at $receivetime\n"; close($client); }#End receiving connection

Replies are listed 'Best First'.
Re: Socket Server Speed
by amphiplex (Monk) on Jul 01, 2002 at 13:52 UTC
    Hi ! There are some things that caught my eye immediately:
    • why do you sleep after each line received ?
    • you use the same substr command 5 times, it would be much more efficient to store the result of substr into a variable once and then use this variable
    • why do you call autoflush after reading a line ? It is sufficient to call it once, as you do anyhow

    ---- kurt
      Thanks, those changes made it much faster. I'm sure the main offender was the sleep. I hadn't ment to do that, just one of those things that I wasn't paying attention to.
Re: Socket Server Speed
by robobunny (Friar) on Jul 01, 2002 at 14:03 UTC
    as amphiplex pointed out, sleeping is your main problem. you could also tidy up that long if(substr( statment up a lot by putting all the stuff you want to compare against in a hash.
    unless(defined($hash_of_strings{substr($_,0,4)})) { # do stuff }
    another point, that has nothing to do with speed, but would make your code a little more readable. the only part of that you can't indent is the part between the EOF's. the rest of the if statement can be indented.
Re: Socket Server Speed
by Ryszard (Priest) on Jul 01, 2002 at 13:51 UTC
    ok, the mandatory, completely useless advice: Get a faster network.. :-)
Re: Socket Server Speed
by Abigail-II (Bishop) on Jul 01, 2002 at 14:33 UTC
    use Time::Space::Continuum;
    (If you were at YAPC::NA::2002, you'd know).

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://178543]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found