根据ip和子网掩码计算广播地址
我想计算以下内容的广播地址:
IP: 192.168.3.1
Subnet: 255.255.255.0
= 192.168.3.255
在 C 语言中。
我知道方法(在反转的 IP 和子网之间进行奇特的按位或),但我的问题是我来自 MacOSX Cocoa 编程的绿色领域。
我研究了 ipcal 的源代码,但无法将其集成到我的代码库中。 互联网上一定有一个简单的十行代码,我只是找不到它。 有人能给我提供一个简短的代码示例来说明如何用 C 语言实现这一点吗?
I want to calculate the broadcast address for:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需计算:(
广播 = ip-addr 或反转的子网掩码)
广播地址有一个
1
位,而子网掩码有一个0
位。Just calculate:
(Broadcast = ip-addr or the inverted subnet-mask)
The broadcast address has a
1
bit where the subnet mask has a0
bit.我知道OP至少对位级算术有一个模糊的理解,但在将字符串转换为数字及其逆时迷失了。 这是一个工作示例(无论如何都进行了最少的测试),使用 froh42 的计算。
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.
可能是吗?
编辑:我认为ip和子网都没有“。”
Could it be?
Edit: I considered that both ip and subnet are without "."
以下是如何在 C# 中执行此操作。 例如,使用 ip 10.28.40.149 和网络掩码 255.255.252.0 返回 10.28.43.255,这是正确的广播地址。 感谢 此处
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
好吧,谁将来会寻找此代码。 我今天有时需要这个,这里是完整的代码,它可以工作:)只需复制并粘贴它,然后导入所需的 dll。
然后就这样称呼它:
快乐编码:)
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.
Then just call it like :
Happy coding :)