Weird socket happenings

C

charlie.bursell

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

Henry Law

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

Mark Clements

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
 
C

charlie.bursell

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

Big and Blue

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

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top