Help with socket timeout

Help with socket timeout

am 09.12.2009 01:44:28 von Barry Brevik

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";
}
}

Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with socket timeout

am 09.12.2009 01:56:55 von Michael Ellery

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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Help with socket timeout

am 09.12.2009 19:37:54 von Ken Cornetet

Ping it first with Net::Ping

-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Barry Brevik
Sent: Tuesday, December 08, 2009 7:44 PM
To: activeperl@listserv.ActiveState.com
Subject: Help with socket timeout

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";
}
}

Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with socket timeout

am 10.12.2009 11:14:19 von Angelos Karageorgiou

This is a multi-part message in MIME format.
--------------010409090008080208040808
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Ken Cornetet wrote:
> Ping it first with Net::Ping
>
> -----Original Message-----
> From: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Barry Brevik
> Sent: Tuesday, December 08, 2009 7:44 PM
> To: activeperl@listserv.ActiveState.com
> Subject: Help with socket timeout
>
> 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.
>

a better way to handle it would be via alarm(ed) timeouts like this

eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required
alarm $timeout;

A lot of socket work done here

alarm 0;
};
die if $@ && $@ ne "alarm\n"; # propagate errors

# the connection is fine here
}

--------------010409090008080208040808
Content-Type: text/x-vcard; charset=utf-8;
name="angelos.vcf"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="angelos.vcf"

begin:vcard
fn:Angelos Karageorgiou
n:Karageorgiou;Angelos
org:Vivodi Telecommunications S.A. / On Telecoms
email;internet:angelos@unix.gr
title:Security Manager
tel;work:+30 211 7503 893
tel;fax:+30 211 7503 701
tel;cell:+30 6949120773
note;quoted-printable: =
=
Linkedin Profile =
=
http://www.linkedin.com/in/unixgr =
=
=
=
Personal Web Site =
http://www.unix.gr =
=
=
Blog Site =
http://angelos-proverbs.blogspot.com
x-mozilla-html:FALSE
url:http://www.linkedin.com/in/unixgr
version:2.1
end:vcard


--------------010409090008080208040808
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--------------010409090008080208040808--

Re: Help with socket timeout

am 10.12.2009 21:34:18 von Bill Luebkert

Angelos Karageorgiou wrote:
>
> a better way to handle it would be via alarm(ed) timeouts like this
>
> eval {
> local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required
> alarm $timeout;
>
> A lot of socket work done here
>
> alarm 0;
> };
> die if $@ && $@ ne "alarm\n"; # propagate errors
>
> # the connection is fine here
> }

Have you tried that on Windoze ? I'm not sure you'll get the
expected results. I don't think timeouts will work on sockets
under Windoze, but you may be able to do something using the
Windows API or using non-blocking I/O on the connect. I don't
have time to experiment right now or I'd give you some better
advice.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Help with socket timeout

am 10.12.2009 22:10:04 von Jeff Saxton

Why not do it the simple way?

use IO::Socket;
my $sock = IO::Socket::INET->new($somewhere);
$sock->timeout($n);




-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Bill Luebkert
Sent: Thursday, December 10, 2009 12:34 PM
To: activeperl@listserv.ActiveState.com
Subject: Re: Help with socket timeout

Angelos Karageorgiou wrote:
>
> a better way to handle it would be via alarm(ed) timeouts like this
>
> eval {
> local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required
> alarm $timeout;
>
> A lot of socket work done here
>
> alarm 0;
> };
> die if $@ && $@ ne "alarm\n"; # propagate errors
>
> # the connection is fine here
> }

Have you tried that on Windoze ? I'm not sure you'll get the
expected results. I don't think timeouts will work on sockets
under Windoze, but you may be able to do something using the
Windows API or using non-blocking I/O on the connect. I don't
have time to experiment right now or I'd give you some better
advice.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with socket timeout

am 10.12.2009 22:24:52 von Bill Luebkert

Jeff Saxton wrote:
> Why not do it the simple way?
>
> use IO::Socket;
> my $sock = IO::Socket::INET->new($somewhere);
> $sock->timeout($n);

Have you tried that on Windoze ?
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Help with socket timeout

am 10.12.2009 22:32:08 von Jeff Saxton

Oops! F***ing M$ appears to not work on windoze :(

-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Bill Luebkert
Sent: Thursday, December 10, 2009 1:25 PM
To: activeperl@listserv.ActiveState.com
Subject: Re: Help with socket timeout

Jeff Saxton wrote:
> Why not do it the simple way?
>
> use IO::Socket;
> my $sock = IO::Socket::INET->new($somewhere);
> $sock->timeout($n);

Have you tried that on Windoze ?
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs