Weird socket happenings

Discussion in 'Perl Misc' started by charlie.bursell@quovadx.com, Dec 27, 2005.

  1. Guest

    I have two Windows 2000 boxes in my home office. On each when I run
    the code below, all I see on the host side is HEX 0000000c which
    is the length only. Then the little program will hang until I kill it.


    The weird thing is that this code works properly on Unix, Windows 2003
    and another Windows 2000 box. I have tested it on all of these.

    Can anyone at least point me in the right direction? This is running
    me up a tree!

    The code below is extracted from other perl utilities that I am trying
    to use but when called will hang as does this little script.

    Here is the code:

    $port = 9988;
    $thishost = localhost;
    $AF_INET = 2;
    $SOCK_STREAM = 1;
    $proto = getprotobyname('tcp');
    $sockaddr = 'S n a4 x8';

    $thataddr = gethostbyname( $thishost );

    # Form net address
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);

    # Make the socket filehandle.
    if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
    print "socket ok\n";
    } else {
    print "Unable to create socket to localhost on port $port\n";
    die "Error was: $!\n";
    }

    # Set socket to be line buffered.
    select(S); $| = 1; select stdout;

    # Connect to the host.
    if (connect(S, $that)) {
    print "Connect ok\n";
    } else {
    print "Unable to contact host '$thishost' on port $port\n";
    die "Error was: $!\n";
    }

    $txt = "Hello World!";
    printf "Sending String: $txt\n";

    # Write length encoding in network byte order, then string.

    $len = length($txt);

    print "Writing $len bytes to conn: '$txt'\n";
    print S pack("N", $len); # 4-byte Network byte order
    print S $txt;
    close S;
    , Dec 27, 2005
    #1
    1. Advertising

  2. Henry Law Guest

    wrote:
    > I have two Windows 2000 boxes in my home office. On each when I run
    > the code below, all I see on the host side is HEX 0000000c which
    > is the length only. Then the little program will hang until I kill it.
    >
    >
    > The weird thing is that this code works properly on Unix, Windows 2003
    > and another Windows 2000 box. I have tested it on all of these.
    >
    > Can anyone at least point me in the right direction? This is running
    > me up a tree!


    Can't help with the Perl socket stuff as I'm not familiar with it. But
    I strongly suspect that's not where the problem lies. Some suggestions
    (which, as you're obviously an adept, you've probably thought of).

    * First, have you tried tracing the traffic on the network? tcpdump is
    available for Windows and may be installed already on your UNIX
    machine. Would be worth seeing - confirming - that the packet is
    correctly formed.

    * You say your "sending" program hangs until you kill it. Where does
    it hang? Does it run under debug? (perl -d, that is). If not,
    have you tried bunging print statements in between every line to
    see which one is executed before the hang?

    * It might be worth distilling the "sender" even more by hard-coding
    your packet (read it out of a file, created with a hex editor,
    maybe) and the IP address, just to eliminate further causes of
    difference. (Can you hear the sound of straws being clutched at?)

    * Fix levels on the "good" and the "renegade" W2K machines? If they're
    different then could you up-level one of renegades to the same
    level as the good one? Same goes for levels of Perl. Could be
    worth up-levelling to the latest build of ActiveState (assuming that's
    what you're running).

    * Could there be some difference in user permissions between the good
    machine and the renegades? Run as "administrator" if you haven't
    already.

    --

    Henry Law <>< Manchester, England
    Henry Law, Dec 27, 2005
    #2
    1. Advertising

  3. wrote:
    > I have two Windows 2000 boxes in my home office. On each when I run
    > the code below, all I see on the host side is HEX 0000000c which
    > is the length only. Then the little program will hang until I kill it.
    >
    >
    > The weird thing is that this code works properly on Unix, Windows 2003
    > and another Windows 2000 box. I have tested it on all of these.
    >
    > Can anyone at least point me in the right direction? This is running
    > me up a tree!
    >
    > The code below is extracted from other perl utilities that I am trying
    > to use but when called will hang as does this little script.
    >
    > Here is the code:
    >
    > $port = 9988;
    > $thishost = localhost;
    > $AF_INET = 2;
    > $SOCK_STREAM = 1;
    > $proto = getprotobyname('tcp');
    > $sockaddr = 'S n a4 x8';
    >
    > $thataddr = gethostbyname( $thishost );
    >
    > # Form net address
    > $that = pack($sockaddr, $AF_INET, $port, $thataddr);



    <snip>

    A few thoughts:

    you need to run with

    use strict;
    use warnings;

    This will enable perl itself to catch a whole slew of potential errors;
    this will save you much grief.

    There is no need to define $AF_INET and $SOCK_STREAM yourself: there are
    constants defined in Socket for this purpose. Check out

    perldoc Socket

    What do you expect to be listening on tcp port 9988? If there isn't
    anything listening on the host to which you are connecting (ie
    localhost), your client script isn't going to connect. What is the exact
    output your script produces?

    > $txt = "Hello World!";

    My initial feeling was that you had misspelt this, but a quick google
    appears to show that the jury is out. This has no bearing on your
    issues, though :)

    Mark
    Mark Clements, Dec 27, 2005
    #3
  4. Guest

    Thanks for the reply. I have tried all of the above, traces, etc..
    Both W2K machines at SP 4.
    Tried the debug, it works up to where the script hangs.

    I have put *MANY* print statements. The script hangs at the "print S"
    statement.

    The point here is, though poorly written, this little script works
    everywhere but on my two machines.
    , Dec 27, 2005
    #4
  5. Big and Blue Guest

    wrote:
    >
    > $port = 9988;
    > $thishost = localhost;
    > $AF_INET = 2;


    No - it isn't. Well, it might be, but it is actually whatever AF_INET
    is defined to be on your system. At leats one system (I forget which) has
    AF_INT (or PF_INET) defined as somethign other then 2.

    > $SOCK_STREAM = 1;


    Similarly for that.

    > $proto = getprotobyname('tcp');
    > $sockaddr = 'S n a4 x8';
    >
    > $thataddr = gethostbyname( $thishost );
    >
    > # Form net address
    > $that = pack($sockaddr, $AF_INET, $port, $thataddr);
    >
    > # Make the socket filehandle.
    > if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
    > print "socket ok\n";
    > } else {
    > print "Unable to create socket to localhost on port $port\n";
    > die "Error was: $!\n";
    > }


    Why not just:

    my $s = IO::Socket::INET->new(Port => $port, ....etc.);

    and re-use the code which is already there?



    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
    Big and Blue, Dec 28, 2005
    #5
  6. Guest

    <> wrote:
    > Thanks for the reply. I have tried all of the above, traces, etc..


    What reply? PLease quote some context.

    Axel
    , Dec 28, 2005
    #6
    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. Laszlo Nagy
    Replies:
    1
    Views:
    4,822
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    966
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    545
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    662
    Steve Holden
    Feb 1, 2009
  5. Knute Johnson

    Stranger printer happenings?

    Knute Johnson, Feb 1, 2014, in forum: Java
    Replies:
    3
    Views:
    117
    Knute Johnson
    Feb 2, 2014
Loading...

Share This Page