Mongers,

So I've recently made my first serious foray into socket programming. It's
pretty cool, it makes me feel 31337.  (Look! you can talk directly to the
whois port!) Others aren't as excited as I am. . .

Anyway, besides always typing "sockey" when I type socket, I'm having one
minor problem.

I'm trying to bind a client program to a specific IP, and I'm having trouble
with the syntax. Normally, I would use IO::Socket, because it's easier, but
in this case I need more control over my socket that IO::Socket would easily
provide.

Anyway, the relevant code is below. I'm trying to automate a set of whois
lookups, and I need to bind to different IPs ocassionally.

Is there anything jumping out at you? The docs aren't that great, the Camel
mentions "you can bind a client to an address too" but then doesn't really
go into details. What I have below seems like the most logical solution but
doesn't work.

Thanks!

-Dan Collis Puro


******************************

use Socket;

my @test=('yahoo.com','whois.networksolutions.com','165.247.62.62');


sub whoislookup2{
    my($domain,$server,$ip) = @_;
    my(@test,$sin);
    socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or return
undef;
    # This builds the address of the remote machine, to be used in the
connect
    my $remote_host=inet_aton($server) or return undef;
    $sin = sockaddr_in(43, $remote_host);

    # This builds the address of the local machine, to be used for binding
the client to a specific IP (this program)
    my $local_host=inet_aton($ip) or return undef;
    my $local = sockaddr_in(43, $local_host);
    bind(SOCK,$local) or print 'Couldn\'t bind to local\n';

    connect(SOCK, $sin) or return undef;
    select((select(SOCK),$| =1)[0]); #This enables buffering on SOCK
    print SOCK "$domain\n";
    @test=<SOCK> or return undef;
    close(SOCK) or return undef;
    return @test;
}

Reply via email to