开发者

How can I tell Perl's IO::Socket::INET which interface to use?

I have two interfaces on my server, eth0 and eth0:0. Those are two different external IP addresses and obviously two different reverse domains.

When I open a IO::Socket::INET connection, Perl uses the eth0 interface by default. I would like to use the second i开发者_运维百科nterface (eth0:0) because this has a different IP and I dont want to use my main IP or domain.

I have absolutely no idea how to select which interface to connect through.

Here's the code I use to open a socket:

my $sock = new IO::Socket::INET(PeerAddr    => $server,
                                PeerPort    => $serverPort,
                                Proto       => 'tcp') or
                             die "Can't connect to server: $!";


You have to give IO::Socket::INET the address of the interface you want to use as LocalAddr parameter. Imagine that 10.0.0.1 is the IP address of eth0 and 10.0.0.2 the IP address of eth0:0, then it would work like this.

my $sock = new IO::Socket::INET(PeerAddr    => $server,
    PeerPort    => $serverPort,
    Proto       => 'tcp'
    LocalAddr   => '10.0.0.2') or
  die "Can't connect to server: $!";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜