Dearest monks,

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 than 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 on the same box 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.

This is possible, right?, to bind a client to a specific IP. It seems like an unusual, but important, thing to be able to do. I mean, if anything can do it, perl can. . .

Thanks!

******************************
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 retur +n 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 bin +ding 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; }

In reply to Bind Socket client to specific IP by Hero Zzyzzx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.