Barry Brevik wrote:
> I am using Active Perl 5.8.8 on Windows.
>  
> I am writing an app that opens a TCP socket to a network printer, and
> then prints barcode labels on it. When the app starts up, it tries to
> determine if the specific printer is reachable or not.
>  
> If the printer is on the network and online, then the test goes well.
> However, if the printer is not reachable for some reason, there is a
> lengthy timeout before the connect function fails.
> 
> I would like to make that timeout much shorter than it is, so I wonder
> if anybody knows how to do that. My sample program is shown below:
> 
>   use Socket;
>   use Warnings;
> 
>   $printPort = 9100;
>   $printHost = '10.18.0.30';
>   $printProtocol = (getprotobyname('tcp'))[2];
> 
>   # The print host can be expressed as either a host name, or an
>   # IP address, but it has to end up as a binary IP address.
>   if ($printHost =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
>   {
>     $printHost = pack('C4', split(/\./, $printHost));
>   }
>   else {$printHost = (gethostbyname($printHost))[4];}
> 
>   $printHostReadable = join('.', unpack('C4', $printHost));
>   print "\n\nConnecting to printer $printHostReadable...  ";
> 
>   if (socket(BARCODE, AF_INET, SOCK_STREAM, $printProtocol))
>   {
>       if (connect(BARCODE, pack('Sna4x8', AF_INET, $printPort,
> $printHost)))
>     {
>       print "<< ok >>\n\n";
>       close BARCODE;
>     }
>     else
>     {
>       print "<< offline >>\n\n";
>     }
>   }
> 

the IO::Socket package has an optional timeout value for connect
(http://perldoc.perl.org/IO/Socket/INET.html). Looking briefly at that
module, it seems to be implemented using select. You can try to do
something like that yourself...or just use the IO::Socket package and be
done with it.

-Mike




_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to