Jump to content

Socket Problems

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
I'm having issues with socket programming; I get unknown errors when I run this 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;

It crashes where indicated with the error message "unknown error". Any ideas?

Edited by dargueta, 04 March 2009 - 06:21 PM.
Fixed formatting


#2
KevinADC

KevinADC

    Programmer

  • Members
  • PipPipPipPip
  • 125 posts
try this:

my $con = openHTTPConnection($target,$me);

use Data::Dumper;

print Dumper \$con;

See if anything gets printed.

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
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.

#4
KevinADC

KevinADC

    Programmer

  • Members
  • PipPipPipPip
  • 125 posts
One problem might be that you are using IO::Socket but want to use IO::Socket::INET, see if this works:


#!/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;



#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
It works just fine if I don't specify the host IP, so I don't think that's the problem.

#6
KevinADC

KevinADC

    Programmer

  • Members
  • PipPipPipPip
  • 125 posts
So it works or doesn't work?

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
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.

#8
KevinADC

KevinADC

    Programmer

  • Members
  • PipPipPipPip
  • 125 posts
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?

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
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.

#10
KevinADC

KevinADC

    Programmer

  • Members
  • PipPipPipPip
  • 125 posts
Don't know what to say. "Unknown error" is obviously pretty hard to diagnose.

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
I realize that. I was kind of hoping someone with better knowledge of Perl or socket communication than me would know the answer.

#12
KevinADC

KevinADC

    Programmer

  • Members
  • PipPipPipPip
  • 125 posts
Maybe try PerlMonks - The Monastery Gates where there are many more perl coders