``````IP:     192.168.3.1
Subnet: 255.255.255.0
=       192.168.3.255
``````

in C.

I know the way (doing fancy bitwise OR's between the inversed IP and subnet), but my problem is I come from the green fields of MacOSX Cocoa programing.

I looked into the source of ipcal, but wasn't able to integrate it into my code base. There must be a simple ten lines of code somewhere on the internet, I just can't find it. Could someone point me to a short code example of how to do it in C?

the Tin Man
• 158,662
• 42
• 215
• 303

Just calculate:

``````broadcast = ip | ( ~ subnet )
``````

The broadcast address has a `1` bit where the subnet mask has a `0` bit.

the Tin Man
• 158,662
• 42
• 215
• 303
froh42
• 5,190
• 6
• 30
• 42

I understand that the OP had at least a vague understanding of the bit-level arithmetic but was lost on converting the strings to numbers and its inverse. here's a working (with minimal testing anyway) example, using froh42's calculation.

``````jcomeau@aspire:~/rentacoder/jcomeau/freifunk\$ cat inet.c; make inet; ./inet 192.168.3.1 255.255.255.0
#include <arpa/inet.h>
#include <stdio.h>
int main(int argc, char **argv) {
char *host_ip = argc > 1 ? argv[1] : "127.0.0.1";
char *netmask = argc > 2 ? argv[2] : "255.255.255.255";
if (inet_pton(AF_INET, host_ip, &host) == 1 &&
else {
fprintf(stderr, "Failed converting strings to numbers\n");
return 1;
}
else {
fprintf(stderr, "Failed converting number to string\n");
return 1;
}
return 0;
}
cc     inet.c   -o inet
``````
jcomeau_ictx
• 37,688
• 6
• 92
• 107

Could it be?

``````unsigned broadcast(unsigned ip,unsigned subnet){
unsigned int bits = subnet ^ 0xffffffff;
unsigned int bcast = ip | bits;

return bcast;
}
``````

Edit: I considered that both ip and subnet are without "."

Tom
• 43,810
• 29
• 138
• 169

Here is how to do it in C#. for example using ip 10.28.40.149 with netmask 255.255.252.0 returns 10.28.43.255 which is the correct broadcast address. thanks to some code from here

``````private static string GetBroadcastAddress(string ipAddress, string subnetMask) {

throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

}
}
``````

ok whom will look for this code in the future. I have spend sometimes today as I needed this, here is the full code and it works :) simply copy and paste it and then import the required dlls.

``````private IPAddress CalculateBroadCastAddress(IPAddress currentIP, IPAddress ipNetMask)
{
string[] strCurrentIP = currentIP.ToString().Split('.');

for (int i = 0; i < 4; i++)
{
int nrBCOct = int.Parse(strCurrentIP[i]) | (int.Parse(strIPNetMask[i]) ^ 255);
}
}

{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
{
{
return ip;

}
}
return null;
}

{
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface interf in Interfaces)
{

{
}

}

return null;

}
``````

Then just call it like :

``````IPAddress  broadcastip = CalculateBroadCastAddress(getIP(), getSubnetMask());
``````

Happy coding :)

Zardaloop
• 1,594
• 5
• 22
• 43