Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Socket Problems

  1. #1
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Socket Problems

    I'm having issues with socket programming; I get unknown errors when I run this code:

    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?
    Last edited by dargueta; 03-04-2009 at 06:21 PM. Reason: Fixed formatting

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: Socket Problems

    try this:

    Code:
    my $con = openHTTPConnection($target,$me);
    use Data::Dumper;
    print Dumper \$con;
    See if anything gets printed.

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Socket Problems

    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.

  5. #4
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: Socket Problems

    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;

  6. #5
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Socket Problems

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

  7. #6
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: Socket Problems

    So it works or doesn't work?

  8. #7
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Socket Problems

    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.

  9. #8
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: Socket Problems

    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?

  10. #9
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Socket Problems

    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.

  11. #10
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: Socket Problems

    Don't know what to say. "Unknown error" is obviously pretty hard to diagnose.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 03-19-2011, 04:13 AM
  2. Java Socket
    By luckytree2011 in forum Java Help
    Replies: 1
    Last Post: 12-17-2010, 07:33 PM
  3. Socket in VB.NET
    By akashj87 in forum Visual Basic Programming
    Replies: 0
    Last Post: 05-04-2010, 06:23 PM
  4. What is Socket.
    By Waseempki in forum General Programming
    Replies: 7
    Last Post: 04-10-2010, 12:49 PM
  5. can php for socket
    By kiddies in forum PHP Development
    Replies: 2
    Last Post: 05-28-2009, 12:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts