Question : Automatically Calculating or finding out the Broadcast address

Dear Friends, i am a programmer (VB6)
i currently am programming a winsock application that connects a few clients to a server.

this is the process:
1. client perform a udp broadcast to find the ip of ther server
2. when the server receives the udp broadcast it replies to the client with his ip and port used to listen
3. then the client connects to the server using tcp on the ip and port specified...

i currently have 2 pcs at home with the current configuration:
pc1:
ip: 192.168.8.1
subnet: 255.255.255.240
default gw: (none)

pc2:
ip: 192.168.8.2
subnet: 255.255.255.240
default gw: 192.168.8.1

i've found out also that my broadcast address is: 192.168.8.15

i used this broadcast address for the clients, and the program worked perfectly!!!
if i use any other broadcast address it wont work.. (except for 192.168.8.255)

now the question is the following:
i would like to have a function that automatically calculates or discovers the broadcast address for the ip/net mask used by the machine

ex:
if my machine has an ip of 192.168.8.1 with subnet 255.255.255.240 it will automatically return

Thanks in advance,

Cheers,
Dan

P.S.
EXPERIMENT:
i remember something from back my college years about "and"ing the ip and subnet mask to check if 2 computers can access each other. but i dont know if there was a rule to calculate the broadcast address:

what i noticed is that both 15 and 255 are able to broadcast to the server on the 240 subnet
240 in binary is:  1111 0000

15 in binary is: 0000 1111
255 in binary is: 1111 1111

the thing in common is they both have the last four digits 1s which might have something to do with the fact that if we OR the broadcast address and the subnet mask we'll have a series of 1s. (1111.1111.1111.1111)

then i tried 31: 0001 1111 for the broadcast, but it didnt work (so much for my theory)

does it have anything to do with 15=255-240??

i just cant find the link, and if there's no link or rule, can someone give me a list of all netmasks and their corresonding broadcast ips?

Answer : Automatically Calculating or finding out the Broadcast address

There is a rule, and it's actually not too complicated.
You take the net mask and invert it, then you "or" the inverted net mask with the IP address.
Random Solutions  
 
programming4us programming4us