I'm having issues with socket programming; I get unknown errors when I run this code:
It crashes where indicated with the error message "unknown error". Any ideas?Code:#!/usr/bin/perl use strict; use warnings; use IO::Socket; # # HTTPCON openHTTPConnection(STRING peer) # sub openHTTPConnection { my ($target,$me) = @_; return new IO::Socket::INET( LocalAddr => $me, PeerAddr => $target, PeerPort => '80', Proto => 'tcp'); } my $target = "<removed>"; #website goes here my $me = "<removed>"; #IP address goes here my $con = openHTTPConnection($target,$me); unless($con) { #CRASHES HERE die("Could not open connection. Reason: $!"); } #open dump file open(HFILE,">C:\\USERS\\DIEGO\\DESKTOP\\OUT.HTML") or die("Could not open buffer file. Reason: $!"); #send HTTP request. printf $con "GET / HTTP/1.1\nHost: $target\n\n"; my ($tmpbuf,$pagebuf) = ("",""); #dump webpage contents into file while(defined($tmpbuf = <$con>)) { printf HFILE $tmpbuf; } #clean up and exit close $con; close HFILE;
Last edited by dargueta; 03-04-2009 at 06:21 PM. Reason: Fixed formatting
try this:
See if anything gets printed.Code:my $con = openHTTPConnection($target,$me); use Data::Dumper; print Dumper \$con;
I get $VAR1 = \undef;. I don't know what that's supposed to mean, but I know undef is roughly the equivalent of NULL in C/C++, so it can't be good.
One problem might be that you are using IO::Socket but want to use IO::Socket::INET, see if this works:
Code:#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; my $target = "<removed>"; #website goes here my $me = "<removed>"; #IP address goes here my $con = IO::Socket::INET->new( LocalAddr => $me, PeerAddr => $target, PeerPort => '80', Proto => 'tcp') or die "Can't bind : $@\n"; #open dump file open(HFILE,">C:\\USERS\\DIEGO\\DESKTOP\\OUT.HTML") or die("Could not open buffer file. Reason: $!"); #send HTTP request. printf $con "GET / HTTP/1.1\nHost: $target\n\n"; my ($tmpbuf,$pagebuf) = ("",""); #dump webpage contents into file while(defined($tmpbuf = <$con>)) { printf HFILE $tmpbuf; } #clean up and exit close $con; close HFILE;
It works just fine if I don't specify the host IP, so I don't think that's the problem.
So it works or doesn't work?
If I comment out the line specifying the host address, it works. If I don't comment it out, then it doesn't. I need the host specified for my program.
Are you getting an error or a message saying "Can't bind ........". If you see the can't bind error that has nothing to do with perl and I don't know why your connection can't be completed. Firewall maybe?
I get the message "unknown error". It's not my firewall (at least I don't think), because it's set to alert me if a program is blocked by the firewall, and I haven't seen any big annoying popups from the UAC.
Don't know what to say. "Unknown error" is obviously pretty hard to diagnose.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks