Hello, I'm writing a small app that sends a message to a server via TCP/SSL and then I need to wait for a full message back from the system. Can anyone see where I can improve the line of "sleep". I tried something using an EVAL, i read this on the internet somewhere, but it doesnt really seem to work.

Any other ideas? Thanks!

#!/usr/local/bin/perl use strict; #Create Socket Connection use IO::Socket::SSL qw(debug3); my $sock = IO::Socket::SSL->new( PeerAddr => 'LOCALHOST', PeerPort => '6999', SSL_version => 'SSLv3', SSL_cipher_list => 'COMPLEMENTOFDEFAULT', SSL_error_trap => 1 ); die "Could not create socket: " . IO::Socket::SSL::errstr() unless $so +ck; #Get the message my $socket_message = shift; print "Message Rec: $socket_message\n"; #Convert the hex message to a string $socket_message = (hex2string($socket_message)); print "Translated Message: $socket_message\n"; print "*Sending Message*\n"; print $sock $socket_message; print "*Message Sent*\n"; my $response; sleep 4; #I wish there was a better way to do this! #This times out the function so I can get everything from the server. $SIG{ALRM} = sub { die "timeout" }; eval { alarm(1); #Change this 1 to something else for more time. while(1){ sysread($sock, $response, 4000); } alarm(0); }; close($sock); print "Server Response: $response\n"; $response = (string2hex($response)); print "Server Response: $response\n"; print "Transaction Completed!"; #Hex Functions sub string2hex { my $in = shift; join(' ', map { sprintf("%02X", ord($_)) } split(//, $in)); } sub hex2string { my $in = shift; join('', map { &convert_word($_) } split(/\s+/, $in)); } sub convert_word { my $word = shift; my $rv = ''; if (length($word) == 2) { $rv = chr(hex($_)); } elsif (length($word) == 1) { $rv = $_; } $rv; } exit;

In reply to Waiting for input, without sleep by Trihedralguy

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.