IP地址CIDR斜线记法求子网信息小工具软件(vs2010+MFC)

来源:互联网 发布:阿里云公网带宽的作用 编辑:程序博客网 时间:2024/06/10 02:31

最近在学习计算机网络,遇到根据斜线记法来确定该IP所在子网的信息,包括:子网号、子网掩码、第一个可用IP、最后一个可用IP、广播地址、可用IP数总量。

因为自己在二进制和十进制转换之间反应比较慢,所以自己决定做一个自动计算子网所有信息的小工具软件:IPTool!

下面是截图:

image_thumb3

还是像以前一样我会介绍自己的思路和部分代码,希望感兴趣的朋友可以多交流~~

前提:

当然要明白这个小工具的实现方法,你必须会自己得出所有的子网信息,如果你都做不出来,就不可能编写一个工具出来替你完成计算。

思路:

网络号:求网络号就是让给出的IP与斜线后面的网络前缀位数如25,相与即可得结果;

子网掩码:把网络前缀8位一组转化成10进制数就行;

第一个可用地址:网络号加1就是了;

最后一个可用地址:主机号全都变成1,然后减1就是

广播地址:最后一个可用地址加1就是

可用IP数量:2^主机号-2

实现:

ip的各部分添加5个int型成员变量;其余的都为CString。

image_thumb9

对于每个编辑控件的设置为UpdateData();TRUE/FALSE参数设置可以参看我的两外介绍VS2010MFC编程文章.

下面列出对网络前缀edit control编写的代码:

主要的想法是:网络前缀/8,保留商和余数,根据商来分别处理5种情况。

建议读者可以把下面的代码复制到VS2010里面,这样的话就有颜色,也比较容易看。

void CIPToolDlg::OnEnChangeEdit5()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialogEx::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here
        UpdateData(TRUE);
    m_ipdiv=div(m_ipmasknum,8);    
    CString strtemp=_T("");
    //ipamount
    if (m_ipmasknum==32)
    {
        m_ipamount=1;
        OnEnChangeEdit11();
    }else
    {
    m_ipamount=(long)pow(2.0,(32-m_ipmasknum))-2;
    OnEnChangeEdit11();
    }
    switch (m_ipdiv.quot)
    {
    case 0:  
        { //netnumber

           //(m_ippart2&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))):是先求出网络前缀的值:(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))
            //然后再与IP相应的一部分相与m_ippart2&;这样就能得出该部分ip的值。注意!!网络前缀是32位ip地址从左到右的1的位数,所以需要先左移位,在相与。    
        strtemp.Format(_T("%d.0.0.0/%d"),(m_ippart1&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))),m_ipmasknum);
        m_netnumber=strtemp;
        OnEnChangeEdit6();
        strtemp.Empty();
        //netmask
        strtemp.Format(_T("%d.0.0.0"),((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem));
        m_netmask=strtemp;
        OnEnChangeEdit7();
        strtemp.Empty();
        //fristip
        strtemp.Format(_T("%d.0.0.1/%d"),(m_ippart1&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))),m_ipmasknum);
        m_fristip=strtemp;
        OnEnChangeEdit8();
        strtemp.Empty();
        //lastip
        strtemp.Format(_T("%d.255.255.254/%d"),((m_ippart1&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem)))+(int)pow(2.0,(8-m_ipdiv.rem))-1),m_ipmasknum);
        m_lastip=strtemp;
        OnEnChangeEdit9();
        strtemp.Empty();
        //brodcastip
        strtemp.Format(_T("%d.255.255.255/%d"),((m_ippart1&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem)))+(int)pow(2.0,(8-m_ipmasknum))-1),m_ipmasknum);
        m_brodcast=strtemp;
        OnEnChangeEdit10();
        strtemp.Empty();
       }break;
    case 1:
        {   //netnumber
            strtemp.Format(_T("%d.%d.0.0/%d"),m_ippart1,(m_ippart2&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))),m_ipmasknum);
            m_netnumber=strtemp;
            OnEnChangeEdit6();
            strtemp.Empty();
            //netmask
            strtemp.Format(_T("255.%d.0.0"),((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem));
            m_netmask=strtemp;
            OnEnChangeEdit7();
            strtemp.Empty();
            //fristip
            strtemp.Format(_T("%d.%d.0.1/%d"),m_ippart1,(m_ippart2&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))),m_ipmasknum);
            m_fristip=strtemp;
            OnEnChangeEdit8();
            strtemp.Empty();
            //lastip
            strtemp.Format(_T("%d.%d.255.254/%d"),m_ippart1,((m_ippart2&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem)))+(int)pow(2.0,(8-m_ipdiv.rem))-1),m_ipmasknum);
            m_lastip=strtemp;
            OnEnChangeEdit9();
            strtemp.Empty();
            //brodcastip
            strtemp.Format(_T("%d.%d.255.255/%d"),m_ippart1,((m_ippart2&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem)))+(int)pow(2.0,(8-m_ipdiv.rem))-1),m_ipmasknum);
            m_brodcast=strtemp;
            OnEnChangeEdit10();
            strtemp.Empty();
        }break;
    case 2:
        { //netnumber
            strtemp.Format(_T("%d.%d.%d.0/%d"),m_ippart1,m_ippart2,(m_ippart3&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))),m_ipmasknum);
            m_netnumber=strtemp;
            OnEnChangeEdit6();
            strtemp.Empty();
            //netmask
            strtemp.Format(_T("255.255.%d.0"),((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem));
            m_netmask=strtemp;
            OnEnChangeEdit7();
            strtemp.Empty();
            //fristip
            strtemp.Format(_T("%d.%d.%d.1/%d"),m_ippart1,m_ippart2,(m_ippart3&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))),m_ipmasknum);
            m_fristip=strtemp;
            OnEnChangeEdit8();
            strtemp.Empty();
            //lastip
            strtemp.Format(_T("%d.%d.%d.254/%d"),m_ippart1,m_ippart2,(((m_ippart3&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))))+(int)pow(2.0,(8-m_ipdiv.rem))-1),m_ipmasknum);
            m_lastip=strtemp;
            OnEnChangeEdit9();
            strtemp.Empty();
            //brodcastip
            strtemp.Format(_T("%d.%d.%d.255/%d"),m_ippart1,m_ippart2,(((m_ippart3&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))))+(int)pow(2.0,(8-m_ipdiv.rem))-1),m_ipmasknum);
            m_brodcast=strtemp;
            OnEnChangeEdit10();
            strtemp.Empty();
        }break;
    case 3:
        {//netnumber
            strtemp.Format(_T("%d.%d.%d.%d/%d"),m_ippart1,m_ippart2,m_ippart3,(m_ippart4&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem))),m_ipmasknum);
            m_netnumber=strtemp;
            OnEnChangeEdit6();
            strtemp.Empty();
            //netmask
            strtemp.Format(_T("255.255.255.%d"),((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem));
            m_netmask=strtemp;
            OnEnChangeEdit7();
            strtemp.Empty();
            //fristip
            strtemp.Format(_T("%d.%d.%d.%d/%d"),m_ippart1,m_ippart2,m_ippart3,(m_ippart4&(((int)pow(2.0,m_ipdiv.rem)-1)<<(8-m_ipdiv.rem)))+1,m_ipmasknum);
            m_fristip=strtemp;
            OnEnChangeEdit8();
            strtemp.Empty();
            //lastip
            strtemp.Format(_T("%d.%d.%d.254/%d"),m_ippart1,m_ippart2,m_ippart3,m_ipmasknum);
            m_lastip=strtemp;
            OnEnChangeEdit9();
            strtemp.Empty();
            //brodcastip
            strtemp.Format(_T("%d.%d.%d.255/%d"),m_ippart1,m_ippart2,m_ippart3,m_ipmasknum);
            m_brodcast=strtemp;
            OnEnChangeEdit10();
            strtemp.Empty();
        }break;
    case 4:
        {//netnumber
            strtemp.Format(_T("%d.%d.%d.%d/32"),m_ippart1,m_ippart2,m_ippart3,m_ippart4);
            m_netnumber=strtemp;
            OnEnChangeEdit6();
            strtemp.Empty();
            //netmask
            strtemp.Format(_T("255.255.255.%d"),255);
            m_netmask=strtemp;
            OnEnChangeEdit7();
            strtemp.Empty();
            //fristip
            strtemp.Format(_T("%d.%d.%d.%d/32"),m_ippart1,m_ippart2,m_ippart3,m_ippart4);
            m_fristip=strtemp;
            OnEnChangeEdit8();
            strtemp.Empty();
            //lastip
            strtemp.Format(_T("%d.%d.%d.%d/32"),m_ippart1,m_ippart2,m_ippart3,m_ippart4);
            m_lastip=strtemp;
            OnEnChangeEdit9();
            strtemp.Empty();
            //brodcastip
            strtemp.Format(_T("%d.%d.%d.%d/32"),m_ippart1,m_ippart2,m_ippart3,m_ippart4);
            m_brodcast=strtemp;
            OnEnChangeEdit10();
            strtemp.Empty();
           
        }break;
    }
}
软件下载地址:

http://download.csdn.net/source/2791305

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 调节天平时指针向右怎么办 香薰蜡烛融化了怎么办 香薰蜡烛挂壁怎么办y 粗蜡烛只烧中间怎么办 紫薯馒头变绿怎么办 小孩手开水烫了怎么办 被油烫伤了怎么办才不留疤 烫伤水泡蹭破了怎么办 烧伤的水泡破了怎么办 烧伤后水泡破了怎么办 烫伤泡破了红肿怎么办 烧伤第二天水泡破了怎么办? 烧伤后换药特别疼怎么办 盐酸溅到皮肤上怎么办 磷性磷酸酶高440怎么办 浓硫酸沾到皮肤上怎么办 浓硫酸溅到皮肤上怎么办 浓硫酸滴到皮肤上怎么办 浓硫酸洒在皮肤上怎么办 浓硫酸溅到眼睛里怎么办 盐酸弄到眼睛了怎么办 稀硫酸进眼睛里怎么办 草酸弄到皮肤上怎么办 大理石被盐酸烧发白怎么办 香薰蜡烛化了怎么办 吸入了大量燃烧纸气体怎么办 狗链条上锈了怎么办 思维迟钝反应慢嘴笨怎么办 小孩思维慢反应迟钝怎么办 苹果4g网络慢怎么办 医院没有号了怎么办啊 fgo宝具动画卡顿怎么办 死刑犯在执行前死亡怎么办 汕头交警 违章扣分怎么办办理 幼儿园家长不保险应该怎么办 csgo掉白银坑了怎么办 错过教资认定现场确认怎么办 乡村建设导致民房开裂怎么办 项目部公章丢了怎么办 手机掉了没有卡怎么办 苹果系统软件删了还是出现怎么办