in reply to Binding to a specific interface

Never mind. Figured it out. :)

inet_aton($eth) is the key. Put the IP address you want to bind the server to into the $eth variable.


See example Code:
#!/usr/bin/perl -Tw use strict; BEGIN { $ENV{PATH} = '/usr/ucb:/bin' } use Socket; use Carp; my $EOL = "\015\012"; my $eth = "your.host"; sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } my $port = shift || 2345; my $proto = getprotobyname('tcp'); $port = $1 if $port =~ /(\d+)/; # untaint port number socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $! +"; setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "s +etsockopt: $!"; bind(Server, sockaddr_in($port, inet_aton($eth))) || die "bind: + $!"; listen(Server,SOMAXCONN) || die "listen: $! +"; logmsg "server started on port $port"; my $paddr; $SIG{CHLD} = \&REAPER; for ( ; $paddr = accept(Client,Server); close Client) { my($port,$iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr,AF_INET); logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $p +ort"; print Client "Hello there, $name, it's now ", scalar localtime, $E +OL; }

20050227 Janitored by Corion: Put code in code tags

Replies are listed 'Best First'.
Re^2: Binding to a specific interface
by merlyn (Sage) on Feb 27, 2005 at 20:18 UTC