Problems with IO::socket hanging on windows (works fine on Unix)

D

doctorj

Hi there,

I am new to Perl, so probably making some schoolboy error. I am
developing a bit of code that opens a socket to an Apache server over a
port and sends a GET request to verify it is up and running. I am
using out-of-the-box Perl 5.6.1. The code works perfectly on
Unix(Solaris) but on Windows it just hangs.

The code gets the port number from a config file then uses this to open
a socket and send the GET request. The code is:

-----------------------

sub check_accept_http_request {

use sigtrap;
use IO::Socket;

$home = $ENV{'AP_HOME'};
$url = shift; print "URL is: $url\n";
$host = shift; print "Host is: $host\n";
$searchstring = "@_"; print "Search string is: $searchstring\n";

open FILE, "$home//install//ports.txt" or die "File ports.txt not
found";

while ($line = <FILE>) {

$i = index $line, $searchstring;
if ( $i == 0 ) {
if ($line =~ /(=)([ ]*)(\S+)/) {
$port = $3;
}
}
}

close FILE;

print "Port is: $port\n";

my $this_socket = new IO::Socket::INET (PeerAddr => $host, Timeout =>
"9", PeerPort => $port, Proto => "tcp", type => SOCK_STREAM);
print "Socket is: $this_socket\n";
if(!$this_socket){
$rcode = $!;
} else {
$get_request = ("GET $url HTTP/1.0\r\n" );
# $this_socket->print ($get_request);
# $this_socket->print("Accept: text/plain\n");
# $this_socket->print("Accept: text/html\n");
# $this_socket->print("UserAgent: LoogBrowser/1.0\n\n");
print $this_socket $get_request;
print $this_socket "Accept: text/plain\n";
print $this_socket "Accept: text/html\n";
print $this_socket "UserAgent: LoogBrowser/1.0\n\n";
$line=($this_socket->getline());
print "$line\n";
if($.==1){
($rcode)=($line=~/^\S+\s(\d+)\s(.*)$/);
}
close $this_socket;
}

print $rcode;
}

check_accept_http_request ("/index.html","localhost","HTTP Server");

---------------

When I run this little sub on Unix it all works a treat:

URL is: /index.html
Host is: localhost
Search string is: HTTP Server
Port is: 7777
Socket is: IO::Socket::INET=GLOB(0x1430bc)
HTTP/1.1 200 OK

But when I run it on windows, it hangs after it makes the socket:

URL is: /index.html
Host is: localhost
Search string is: HTTP Server
Port is: 7777
Socket is: IO::Socket::INET=GLOB(0x22e4a0)
<hangs indefinitely here>

Can anyone tell me what I am doing wrong? I'd be grateful for any help
you can provide.

Thanks,
Jon
 
S

Sherm Pendley

Hi there,

I am new to Perl, so probably making some schoolboy error. I am
developing a bit of code that opens a socket to an Apache server over a
port and sends a GET request to verify it is up and running. I am
using out-of-the-box Perl 5.6.1.

Any particular reason you're doing this by hand, instead of using the LWP
module? Not a criticism, just curious - you're putting a *lot* of effort
into re-inventing that wheel.

sherm--
 
D

doctorj

Not particularly, Sherm. I hacked this out of something else I found.
Will have a look at the LWP module now.

Thanks,
Jon
 
D

doctorj

Is LWP a default module? I will be running this thing on our clients'
installations and may not have the opportunity (or permission) to be
adding extra packages etc.

Thanks,
Jon
 
S

Sherm Pendley

Is LWP a default module? I will be running this thing on our clients'
installations and may not have the opportunity (or permission) to be
adding extra packages etc.

No, it's a CPAN module.

Your clients should be easy to convince though - just compare what it
would cost them to pay you for about ten minutes to install LWP vs.
the time it would take you to re-invent it.

sherm--
 
D

doctorj

Hi Sherm,

I tried using LWP and it works a treat on both platforms. Strangely
the module seems to already be present by default in the Perl
installations I am using. This is possibly becuase they come as part
of an Oracle software installation and Oracle probably added them
already.

Thanks again for your advice! Much appreciated.

Jon
 
S

Sherm Pendley

I tried using LWP and it works a treat on both platforms. Strangely
the module seems to already be present by default in the Perl
installations I am using.

Oh, right, I forgot! LWP's not a "core" module that distributed as
part of the Perl source, but it's included with ActiveState's Perl
for Windows.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top