Can a network client specify an IP on the source side?

Discussion in 'Perl Misc' started by yong321@yahoo.com, Apr 28, 2007.

  1. Guest

    When I initiate a network connection to a remote server, is there a
    way to specify an IP address on the source side? Take a Perl snippet
    as an example (modified from "Programming Perl"):

    #!/usr/local/bin/perl -w
    require 5.002;
    use Socket;

    $remote = shift || 'locahost';
    $port = shift || 2345;
    $iaddr = inet_aton($remote) or die "No host: $remote";
    $paddr = sockaddr_in($port, $iaddr);
    $proto = getprotobyname("tcp");
    socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";

    #Does it make sense to bind the socket with an IP on the source? How?
    bind(SOCK, sockaddr_in(0, "10.6.143.111")) or die "bind: $!";

    connect(SOCK, $paddr) or die "connect: $!";
    print while <SOCK>;
    close SOCK or die "close: $!";
    exit;

    I've never seen a bind() call on a client. I added it because I want
    to designate a specific IP on a multihomed server. But it throws
    error:

    $ client.pl some.destination.example.com 22
    bind: Cannot assign requested address at client.pl line <line number
    pointing to the bind() line>.

    That IP does exist as shown in ifconfig output. I suspect the first
    argument to sockaddr() should not be 0. Any help is appreacited.

    Yong Huang
    [This message was posted to another group without a definitive answer]
    , Apr 28, 2007
    #1
    1. Advertising

  2. Guest

    On Apr 28, 10:34 pm, wrote:
    > When I initiate a network connection to a remote server, is there a
    > way to specify an IP address on the source side? Take a Perl snippet
    > as an example (modified from "Programming Perl"):
    >
    > #!/usr/local/bin/perl -w
    > require 5.002;
    > use Socket;
    >
    > $remote = shift || 'locahost';
    > $port = shift || 2345;
    > $iaddr = inet_aton($remote) or die "No host: $remote";
    > $paddr = sockaddr_in($port, $iaddr);
    > $proto = getprotobyname("tcp");
    > socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
    >
    > #Does it make sense to bind the socket with an IP on the source? How?
    > bind(SOCK, sockaddr_in(0, "10.6.143.111")) or die "bind: $!";
    >
    > connect(SOCK, $paddr) or die "connect: $!";
    > print while <SOCK>;
    > close SOCK or die "close: $!";
    > exit;
    >
    > I've never seen a bind() call on a client. I added it because I want
    > to designate a specific IP on a multihomed server. But it throws
    > error:
    >
    > $ client.pl some.destination.example.com 22
    > bind: Cannot assign requested address at client.pl line <line number
    > pointing to the bind() line>.
    >
    > That IP does exist as shown in ifconfig output. I suspect the first
    > argument to sockaddr() should not be 0. Any help is appreacited.
    >
    > Yong Huang
    > [This message was posted to another group without a definitive answer]


    In case anybody is interested, this works, thanks to liyimin41's
    offline help:

    bind(SOCK,sockaddr_in(0,inet_aton("10.6.143.111"))) or die "bind: $!";

    Yong Huang
    , Apr 29, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boss302
    Replies:
    0
    Views:
    1,041
    Boss302
    Nov 21, 2006
  2. Bogdan
    Replies:
    2
    Views:
    649
    Bogdan
    Jun 9, 2008
  3. Zoe Hart
    Replies:
    1
    Views:
    366
    Scott Wisniewski
    Jan 8, 2004
  4. Jack
    Replies:
    4
    Views:
    187
    Phillip Windell
    Nov 26, 2003
  5. Jack
    Replies:
    3
    Views:
    206
Loading...

Share This Page