DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: vic_168999
今日帖子: 7
在线用户: 17
导航: 论坛 -> 网络通讯 斑竹:liumazi,sephil  
作者:
男 net1999 (好人) ★☆☆☆☆ -
普通会员
2023/9/4 20:50:19
标题:
通过本机的IP地址和子网掩码来计算广播地址的代码哪里有? 浏览:919
加入我的收藏
楼主: 就是已知: 本机IP是 192.168.68.1 ,子网掩码是 255.255.255.224, 返回广播地址为: 192.168.68.31。网上找了好久也没有答案。
目前暂时使用 255.255.255.255 代替,不知道有什么不良后果。
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/4 21:35:40
1楼: IP  192.168.40.30 / 27

1) transform in binary value = 0 and 1 values
2 ) transform the "mask" in binary
.... / 27 = n BITs used as network prefix
....... = 8 + 8 + 8 + 3 = 27  .... xxx.xxx.xxx.30

3)  to "NETWORK" do the "AND" calculating using "IP" binary values


4) to "BROADCAST" do the "OR" calculating using the "NETWORK" binary value
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 akuan54 (SKL) ★☆☆☆☆ -
普通会员
2023/9/5 9:19:47
2楼: // IP4 長整數轉成字串
function IntToIp4Str(iAddress: Integer): string;
begin
  Result := Format( '%d.%d.%d.%d', [iAddress shr 24 and $ff,
            iAddress shr 16 and $ff,
            iAddress shr 8  and $ff,
            iAddress shr 0  and $ff]);
end;

// IP4 4個Bytes 轉成整數
function ByteToIP4Int(const A, B, C, D: Byte): Integer;
begin
  Result := (A shl 24) + (B shl 16) + (C shl 8) + D;
end;



// IP4 字串轉成整數
function StrToIP4Int(const AIP4Str: string): Integer;
var
  S: TStrings;
begin
  S := TStringList.Create;
  try
    S.Delimiter := '.';
    S.DelimitedText := AIP4Str;

    // do preeliminary check. The IP4 Address string must consists of 4 parts. Less or more than that would be invalid values
    if S.Count<>4 then
      raise Exception.Create('Invalid IP4 Address string');

    Result := ByteToIP4Int(StrToInt(S[0]), StrToInt(S[1]), StrToInt(S[2]),  StrToInt(S[3]));
  finally
    S.Free;
  end;
end;

// 依照IP, SubnetMask 計算出 IP4 Broadcast IP
// 就是網段中末尾IP,作為廣播使用
function calBroadcastIP(sIP, sMask:String):String;
var iIP, iMask : integer;
    iBroadCast : integer;
begin
    iIP := StrToIP4Int(sIP);
    iMask := StrToIP4Int(sMask);
    iBroadCast := iIP or (not iMask);
    Result := IntToIp4Str(iBroadCast);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
   edit3.text := calBroadcastIP(Edit1.text, Edit2.text);
end;
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/5 9:30:08
3楼: dont use TSTRING for this, use a simple ARRAY

var 
    LOctects : TArray<string> := sValue.Split( [ '.' ], TStringSplitOptions.ExcludeEmpty );

    if ( Lenght( LOctects ) <> 4 ) then
        exit;

     LOctects[ 0 ] ..... [ 3 ]
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 net1999 (好人) ★☆☆☆☆ -
普通会员
2023/9/5 13:19:37
4楼: think you very much! It works well!!!

dont use TSTRING for this, use a simple ARRAY=======> why?
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/5 20:38:43
5楼: @net1999

Why kill a fly with a nuclear missile?
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 pp0123 (pp0123) ★☆☆☆☆ -
普通会员
2023/9/6 2:23:02
6楼: @emailx45

Because akuan54's code is here, but your code not. :P
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/6 2:58:24
7楼: @pp0123

Im preparing a "class" with some code, and give a time to test it.
Then I'll post here

:))
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 hs_kill (lzl_17948876) ★☆☆☆☆ -
普通会员
2023/9/6 9:15:52
8楼: 子网掩码实际的效果是规定了同一网段的IP数量, 其中第一个IP是网段IP, 最后一个IP就是广播IP

注意上面说的数量这个东西, 子网掩码最终就是要计算这个数量, 这个数量就是子网掩码的二进制表示方式中的0的数量
不同的表示方式计算方式也不同

子网掩码常见有2中表达方式 一种是xxx.xxx.xxx.xxx的形式, 常见就是255.255.255.0, 这种, 对应到二进制就是 11111111.11111111.11111111.00000000
有8个0, 所以每个网段的数量就是2^8=256个, 所以网段IP是xxx.xxx.xxx.0, 广播IP是xxx.xxx.xxx.255

同理 如果是255.255.255.252, 那么二进制就是11111111.11111111.11111111.11111100, 有2个0, 所以每个网段数量=2^2=4, 所以就是每4个IP一个网段
那么就要先根据你的IP, 得到你所在的网段, 取网段最后一个IP就是广播地址
比如你是192.168.1.27, 那么所在的网段是:
27 div 4=6
6*4=24
你的网段IP是 24..27(24 + 4 - 1), 很显然27就是广播地址, 所以你的IP是不可用的, 需要改一个

另外一种就是24 26这种表示方式了, 这个值表示了有多少个1, 比如24等于24个1, 子网掩码最多是32个1: 11111111.11111111.11111111.11111111   8*4=32
所以24表示每个网段数量=2^(32-24)=2^8=256

其实24/26这种表示方式会更合适一些, 为什么呢, 上面说了, 子网的数量是根据子网掩码中0的个数来计算的, 其实还有个规则, 就是其中的1必须是连续的
也就是说255.255.255.159(11111111.11111111.11111111.10011111)这种是无效的子网掩码, 但是如果用xxx.xxx.xxx.xxx很难限制到1必须连续, 输入错了就会有问题
而24这种就简单了, 只描述了1的个数, 和连续没关系, 所以就不会出错, 而且描述起来更简洁

原理说完了, 在说说楼主的要求
192.168.68.1, 子网掩码是 255.255.255.224
224=%11100000, 包含5个0, 那么每网段IP数量=2^5=32

1 div 32 = 0 //所在的网段组序号
0{网段组序号} * 32 = 0 //所在网段组的第一个IP
0{第一个IP} + 32 - 1 = 31 //所在网段组的最后一个IP

所以192.168.68.1的广播地址=192.168.68.31
----------------------------------------------
http://www.cnblogs.com/lzl_17948876/
作者:
男 hs_kill (lzl_17948876) ★☆☆☆☆ -
普通会员
2023/9/6 9:37:55
9楼: 啊 补充下啊, 以上只是子网掩码的原理

实际计算起来不需要这么麻烦, 就按1楼2楼说的那样 IP or (not 掩码) 就行了

顺便想一下, IP段(第一个地址)怎么计算得到?
----------------------------------------------
http://www.cnblogs.com/lzl_17948876/
作者:
男 pp0123 (pp0123) ★☆☆☆☆ -
普通会员
2023/9/6 10:16:07
9楼: 广播地址有什么真实的用途?
----------------------------------------------
-
作者:
男 net1999 (好人) ★☆☆☆☆ -
普通会员
2023/9/6 22:46:25
10楼: 使用 udp 发送一个消息给广播地址,本网段的所有机器都接收到。
----------------------------------------------
-
作者:
男 supermay (supermay) ★☆☆☆☆ -
盒子活跃会员
2023/9/7 15:14:14
11楼: 255.255.255.255是全网吧,
255-224=31呀,这个就是可以最简单的算出来了
----------------------------------------------
链接:https://pan.baidu.com/s/12jzmECYKhGCsHBxz8tmB6w 提取码:pelr --来自百度网盘超级会员V9的分享
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行74.21875毫秒 RSS