Re: UDP broadcast with 2 adapters
- From: "Luke Alcatel" <nobody@xxxxxxxxxx>
- Date: Thu, 30 Mar 2006 13:21:19 -0500
Denville,
I don't think the answers you got previously are right and I don't think
your "receive" code does what you think it does.
You have the comment "binds to all ip's" where you used INADDR_ANY. That is
not the effect of using INADDR_ANY in your bind call. The effect is that
you are letting the system decide which local interface to bind to (assuming
there is more than one). A bind only binds to one address and it is always
a local address. A connect deals with a remote address.
If I have understood you correctly, what you really want to do is use the
address of one of the two physical NICs on your PC in the bind call. This
will mean that any broadcast you do on the associated socket will go out
only to the network to which that NIC belongs.
Luke
"Denville Longhurst" <Denville@xxxxxxxxxxx> wrote in message
news:fGuUf.6395$g76.4519@xxxxxxxxxxxxxxxxxxxxxxx
Hello
I use UDP broadcasts on a small network to synchronise applications and
share info - all without problems, lon-term stable.
I broadcast the info on specific UDP ports:
SockAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST) ;
SockAddr.sin_port = htons( (unsigned short)nPort ) ;
SockAddr.sin_family = AF_INET ;
Socket = socket( AF_INET, SOCK_DGRAM, 0 ) ;
status = setsockopt( Socket, SOL_SOCKET, SO_BROADCAST,
(char *)&enable, sizeof(enable) ) ;
....
nSent = sendto( Socket, msg, len, 0,
(LPSOCKADDR)&SockAddr, sizeof(SockAddr) ) ;
And I receive broadcasts thus:
SockAddr.sin_port = htons( (unsigned short)nPort ) ;
SockAddr.sin_family = AF_INET ;
SockAddr.sin_addr.s_addr = htonl(INADDR_ANY) ; // binds to all ip's
(multiple receipts if multiple IP's !)
Socket = socket( AF_INET, SOCK_DGRAM, 0 ) ;
status = bind( Socket, (LPSOCKADDR)&SockAddr, sizeof(SockAddr) ) ;
n = recvfrom( Socket, buf, max, 0, &sAddr, &nsAddr ) ;
Now, I have to use a couple of PC's that have two physical network cards.
One network is our small network as usual; the other is big and unknown
(completely different sub-net of course). So, question is:
How do I ensure that 'my' UDP sends on, and receives on, only our network
interface, and neither broadcasts nor listens on t'other ? The system
should configure automatically as far as possible with the minimal amount of
network-specific information, just enough to discriminate generaly between
the two and then to detect and use 'our' interface.
Many thanks in advance,
Denville.
.
- References:
- UDP broadcast with 2 adapters
- From: Denville Longhurst
- UDP broadcast with 2 adapters
- Prev by Date: Re: Change IP address of sending and receiving packets in driver level
- Next by Date: Listing connected and disconnected ips...any ideas
- Previous by thread: Re: UDP broadcast with 2 adapters
- Next by thread: Re: Flush TCP socket
- Index(es):
Relevant Pages
|