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



On Apr 28, 10:34 pm, yong...@xxxxxxxxx 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

.



Relevant Pages

  • Re: Java Socket Constructor
    ... bind() simply binds a socket to an interface/port locally for both client ... After that you can connectto a server. ...
    (comp.lang.java.programmer)
  • Re: How to listen to more a specific IP and specific ports
    ... I have read this long time ago in the textbook Unix Network Programming ... service and connect our socket to INADDR_ANY and then listen for incoming ... Its clear to me on how to bind with ... port numbers. ...
    (microsoft.public.win32.programmer.networks)
  • Re: UDP question.
    ... > do you mean that I alloc two socket and bind different IP is not ... it will go out the first interface. ... not real bind the adapter? ...
    (Linux-Kernel)
  • Re: UDP question.
    ... >> My two adapters has two different IP address, and I bind one IP ... >> do you mean that I alloc two socket and bind different IP is not ... > sending a packet *to* 1.2.4.5, it will go out the first interface. ... not real bind the adapter? ...
    (Linux-Kernel)
  • RE: Problems with SO_REUSEADDR
    ... The SO_REUSEADDR socket option serves four different purposes: ... SO REUSEADDR allows a listening server to start and bind its well-known ... port even if previously established connections exist that use this port as ... A listening server is started. ...
    (microsoft.public.win32.programmer.networks)