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

Discussion in 'Perl Misc' started by doctorj@blueyonder.co.uk, Jun 2, 2005.

  1. Guest

    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
     
    , Jun 2, 2005
    #1
    1. Advertising

  2. Re: Problems with IO::socket hanging on windows (works fine onUnix)

    writes:

    > 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--
     
    Sherm Pendley, Jun 2, 2005
    #2
    1. Advertising

  3. Guest

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

    Thanks,
    Jon
     
    , Jun 2, 2005
    #3
  4. Guest

    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
     
    , Jun 2, 2005
    #4
  5. Re: Problems with IO::socket hanging on windows (works fine onUnix)

    writes:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jun 2, 2005
    #5
  6. Guest

    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
     
    , Jun 2, 2005
    #6
  7. Re: Problems with IO::socket hanging on windows (works fine onUnix)

    writes:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jun 2, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mufasa
    Replies:
    2
    Views:
    351
    Michael Nemtsev
    Apr 19, 2007
  2. Lord of Chaos
    Replies:
    0
    Views:
    455
    Lord of Chaos
    Jan 11, 2010
  3. ian douglas
    Replies:
    5
    Views:
    190
    88888 Dihedral
    Mar 5, 2013
  4. Wolfgang Strobl
    Replies:
    0
    Views:
    143
    Wolfgang Strobl
    Mar 4, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    158
    Terry Reedy
    Mar 4, 2013
Loading...

Share This Page