C語言中判斷兩個IPv4地址是否屬于同一個子網(wǎng)的代碼
問題描述:
現(xiàn)給定兩個IPv4地址,和一個子網(wǎng)掩碼,判斷是否屬于同一個子網(wǎng),若屬于,輸出1,否則輸出0。
例如輸入:
172.16.1.3
172.16.1.35
255.255.255.224
輸出:
0
解決方案:
首先將字符串格式的IP地址轉(zhuǎn)化為4字節(jié)的IP地址,然后使用與(&)運算,分別將兩個IP地址與掩碼相與,若最后的值相同,則為同一個子網(wǎng),否則不是。
以下函數(shù)的作用是將字符串格式的IP轉(zhuǎn)化為4字節(jié)的IP(因為是4字節(jié),所以使用int,但不同平臺的int所占的字節(jié)好像不同哈~不太確定)
int _to_int(char * str, int start_idx, int end_idx)
{
 int a = 0, i;
 for (i = start_idx; i <= end_idx; ++i)
 {
 a = a * 10 + (str[i] - '0');
 }
 return a;
}
/*
 * 將ip字符串轉(zhuǎn)化為4字節(jié)的整形
 */
int ip_to_int(char * ip)
{
 int start = 0, i = 0, ret = 0;
 int shift_factor = 3; // 一開始要向右移動3 * 8位
 char c;
 while (c = ip[i])
 {
 if (c == '.')
 {
  int a = _to_int(ip, start, i - 1);
  int temp = shift_factor * 8;
  ret = ret | (a << temp);
  shift_factor--;
  start = i + 1;
 }
 i++;
 }
 return ret;
}
_to_int()函數(shù)的作用是將一段字符串轉(zhuǎn)化為數(shù)字,實際上就是將點分隔的字符串轉(zhuǎn)化為數(shù)字,ip_to_int()函數(shù)將字符串格式的ip轉(zhuǎn)化為整形。
以下是ip地址與子網(wǎng)掩碼運算的部分:
#include <stdio.h>
int main()
{
 char a1[15], a2[15], a3[15];
 gets(a1);
 gets(a2);
 gets(a3);
 int ip1 = ip_to_int(a1);
 int ip2 = ip_to_int(a2);
 int ip3 = ip_to_int(a3);
 int result = 0;
 if ((ip1 & ip3) == (ip2 & ip3))
 {
 result = 1;
 }
 printf("%d", result);
 return 0;
}
下面是其它網(wǎng)友的補充
題目描述
子網(wǎng)掩碼是用來判斷任意兩臺計算機的IP地址是否屬于同一子網(wǎng)絡(luò)的根據(jù)。
子網(wǎng)掩碼與IP地址結(jié)構(gòu)相同,是32位二進制數(shù),其中網(wǎng)絡(luò)號部分全為“1”和主機號部分全為“0”。利用子網(wǎng)掩碼可以判斷兩臺主機是否中同一子網(wǎng)中。若兩臺主機的IP地址分別與它們的子網(wǎng)掩碼相“與”后的結(jié)果相同,則說明這兩臺主機在同一子網(wǎng)中。
示例:
I P 地址  192.168.0.1
子網(wǎng)掩碼  255.255.255.0
轉(zhuǎn)化為二進制進行運算:
I P 地址 11010000.10101000.00000000.00000001
子網(wǎng)掩碼 11111111.11111111.11111111.00000000
AND運算
    11000000.10101000.00000000.00000000
轉(zhuǎn)化為十進制后為:
    192.168.0.0
I P 地址  192.168.0.254
子網(wǎng)掩碼  255.255.255.0
轉(zhuǎn)化為二進制進行運算:
I P 地址 11010000.10101000.00000000.11111110
子網(wǎng)掩碼 11111111.11111111.11111111.00000000
AND運算
     11000000.10101000.00000000.00000000
轉(zhuǎn)化為十進制后為:
     192.168.0.0
通過以上對兩臺計算機IP地址與子網(wǎng)掩碼的AND運算后,我們可以看到它運算結(jié)果是一樣的。均為192.168.0.0,所以這二臺計算機可視為是同一子網(wǎng)絡(luò)。
/* 
* 功能: 判斷兩臺計算機IP地址是同一子網(wǎng)絡(luò)。 
* 輸入?yún)?shù): String Mask: 子網(wǎng)掩碼,格式:“255.255.255.0”; 
* String ip1: 計算機1的IP地址,格式:“192.168.0.254”;
* String ip2: 計算機2的IP地址,格式:“192.168.0.1”;
* 
* 返回值: 0:IP1與IP2屬于同一子網(wǎng)絡(luò); 1:IP地址或子網(wǎng)掩碼格式非法; 2:IP1與IP2不屬于同一子網(wǎng)絡(luò)
*/ 
public int checkNetSegment(String mask, String ip1, String ip2) 
{ 
/*在這里實現(xiàn)功能*/
return 0;
}
輸入描述:
輸入子網(wǎng)掩碼、兩個ip地址
輸出描述:
得到計算結(jié)果
輸入例子:
255.255.255.0
192.168.224.256
192.168.10.4
輸出例子:
1
解答代碼:
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef struct ip
{
 int first;
 int second;
 int three;
} IP;
int judgeIp(string ipSubNet,IP &ip)
{
 int index=0;
 ip.first=atoi(&ipSubNet[index]);
 if(ip.first>255)
 return 0;
 index++;
 index=ipSubNet.find_first_of('.',index);
 ip.second=atoi(&ipSubNet[++index]);
 if(ip.second>255)
 return 0;
 index++;
 index=ipSubNet.find_first_of('.',index);
 ip.three=atoi(&ipSubNet[++index]);
 if(ip.three>255)
 return 0;
 index++;
 index=ipSubNet.find_first_of('.',index);
 ip.fouth=atoi(&ipSubNet[++index]);
 if(ip.fouth>255)
 return 0;
 return 1;
}
int main()
{
 string ipSubNet,ipAdd1,ipAdd2;
 IP subNet,ip1,ip2;
 while(cin>>ipSubNet>>ipAdd1>>ipAdd2)
 {
 if(judgeIp(ipSubNet,subNet)&&judgeIp(ipAdd1,ip1)&&judgeIp(ipAdd2,ip2))
 {
  ip1.first=ip1.first & subNet.first;
  ip1.second=ip1.first & subNet.second;
  ip1.second=ip1.first & subNet.second;
  ip1.fouth=ip1.first & subNet.fouth;
  ip2.first=ip2.first & subNet.first;
  ip2.second=ip2.first & subNet.second;
  ip2.second=ip2.first & subNet.second;
  ip2.fouth=ip2.first & subNet.fouth;
  if(ip1.first==ip2.first&&ip1.second==ip2.second&&ip1.three==ip2.three&&ip1.fouth==ip2.fouth)
  cout<<'0'<<endl;
  else
  cout<<'2'<<endl;
 }
 else
  cout<<'1'<<endl;
 }
 return 0;
}
C語言——如何判斷兩個IP在同一網(wǎng)段
ip_addr.h
#define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
                       (mask)->addr) == \
                       ((addr2)->addr & \
                       (mask)->addr))
在程序中,那個“\”表示它之前的程序和后面的是連接的,下一行和上一行是一個語句, 反斜杠符號起到長代碼分行書寫功能。
注意:C語言中的關(guān)鍵字不可以用“\”分行!
到這里,這篇關(guān)于C語言中判斷ip是否在同一子網(wǎng)的文章就結(jié)束到這。
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
 - 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機閱讀
- 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10C#中split用法實例總結(jié)
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 


