Coding for Multiple IP addresses

Coding for Multiple IP addresses

Post by csteo » Fri, 05 Oct 2001 12:53:30



Hi:

I plan to have a machine with 3 IP addresses and I run my app to listen to
port 9000.  How to do socket programming on handling multiple IP to listen
to same port? Is it using "bind()" and use "select()" to pass in the three
socket handle?

I can't try on my own since I don't own multiple IP machine, could anybody
tell me that  what I mention on above correct?

Thanks.

Regards,
Chin Siang

 
 
 

Coding for Multiple IP addresses

Post by ken_yap_31322742_.. » Fri, 05 Oct 2001 13:19:47


[GCC groups removed, this has nothing to do with GCC.]

|I plan to have a machine with 3 IP addresses and I run my app to listen to
|port 9000.  How to do socket programming on handling multiple IP to listen
|to same port? Is it using "bind()" and use "select()" to pass in the three
|socket handle?

If 3 addresses are all that the machine has, just do a bind with
INADDR_ANY and it will bind to all local interfaces (see man 7 ip). Then
a request on any interface will go to your socket. To tell which
interface a request came in on, look at the peer address returned by
accept, or with getpeername if you have the socket already.

If you have more than 3 addresses, and you want to listen on only a
subset, then you have to bind to each separately and poll with select.

 
 
 

Coding for Multiple IP addresses

Post by David Schwart » Sat, 06 Oct 2001 03:19:11


Quote:> If 3 addresses are all that the machine has, just do a bind with
> INADDR_ANY and it will bind to all local interfaces (see man 7 ip). Then
> a request on any interface will go to your socket. To tell which
> interface a request came in on, look at the peer address returned by
> accept, or with getpeername if you have the socket already.

        You can't tell from the peer address return in accept or from
getpeername, what you need is 'getsockname' on the connected socket.

        DS

 
 
 

Coding for Multiple IP addresses

Post by Chris Liecht » Sat, 06 Oct 2001 05:12:35




Quote:> Hi:

> I plan to have a machine with 3 IP addresses and I run my app to listen
> to port 9000.  How to do socket programming on handling multiple IP to
> listen to same port? Is it using "bind()" and use "select()" to pass in
> the three socket handle?

I don't know the C semantics but ususaly you use "bind" to install your
server socket and as argument you can give an IP address, which will mean
that you bind *only* to that address. If you want to bind to *all* network
interfaces you use "INADDR_ANY" (0.0.0.0).

if you have linux try "man 7 ip", "man 2 bind", "man socket"

Quote:> I can't try on my own since I don't own multiple IP machine, could
> anybody tell me that  what I mention on above correct?

Yes you do, if you have one ethernet card or dialup. The other address is
127.0.0.1 and is called "localhost". Its the local loopback device which
lets you test any network software on the local machine.

Quote:> Thanks.

> Regards,
> Chin Siang

--