A module I wrote at work does a rough-approximation of detecting if X11 service is available. It first checks DISPLAY, then it tries to connect a socket to the right port. (Sorry, can't give the whole module.)
use Socket qw(SOCK_DGRAM SOCK_STREAM SOCK_RAW PF_INET inet_aton sockad
+dr_in);
use FileHandle;
# ... other module junk ...
# $spec = display();
#
sub display
{
return $ENV{DISPLAY} || ':0';
}
# $bool = scan($hostname, $tcpport);
#
sub scan
{
my $host = shift;
my $port = shift;
my $proto = (getprotobyname('tcp'))[2];
my $fh = new FileHandle();
my $ip = inet_aton($host);
return undef
if not defined $ip;
return undef
if not socket($fh, &PF_INET(), &SOCK_STREAM(), $proto);
my $saddr = sockaddr_in($port, $ip);
return undef
if not connect($fh, $saddr);
close($fh);
return 1;
}
# $bool = ready();
#
sub ready
{
my $display = display();
my ($host, $screen) = split(/:/, $display);
$host ||= 'localhost';
return scan($host, 6000 + int($screen));
}
There are other situations where the port in question is not so easily computed; these usually relate to tunneling of some sort, such as with (ssh -X) commands. Extend as desired.
--
[ e d @ h a l l e y . c c ]