C語(yǔ)言中怎么判斷兩個(gè)IPv4地址是否屬于同一個(gè)子網(wǎng)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)C語(yǔ)言中怎么判斷兩個(gè)IPv4地址是否屬于同一個(gè)子網(wǎng),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、綏陽(yáng)ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的綏陽(yáng)網(wǎng)站制作公司

問(wèn)題描述:

現(xiàn)給定兩個(gè)IPv4地址,和一個(gè)子網(wǎng)掩碼,判斷是否屬于同一個(gè)子網(wǎng),若屬于,輸出1,否則輸出0。

例如輸入:

172.16.1.3

172.16.1.35

255.255.255.224

輸出:

0

解決方案:

首先將字符串格式的IP地址轉(zhuǎn)化為4字節(jié)的IP地址,然后使用與(&)運(yùn)算,分別將兩個(gè)IP地址與掩碼相與,若最后的值相同,則為同一個(gè)子網(wǎng),否則不是。

以下函數(shù)的作用是將字符串格式的IP轉(zhuǎn)化為4字節(jié)的IP(因?yàn)槭?字節(jié),所以使用int,但不同平臺(tái)的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; // 一開(kāi)始要向右移動(dòng)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ù)字,實(shí)際上就是將點(diǎn)分隔的字符串轉(zhuǎn)化為數(shù)字,ip_to_int()函數(shù)將字符串格式的ip轉(zhuǎn)化為整形。

以下是ip地址與子網(wǎng)掩碼運(yùn)算的部分:

#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)友的補(bǔ)充

題目描述

子網(wǎng)掩碼是用來(lái)判斷任意兩臺(tái)計(jì)算機(jī)的IP地址是否屬于同一子網(wǎng)絡(luò)的根據(jù)。
子網(wǎng)掩碼與IP地址結(jié)構(gòu)相同,是32位二進(jìn)制數(shù),其中網(wǎng)絡(luò)號(hào)部分全為“1”和主機(jī)號(hào)部分全為“0”。利用子網(wǎng)掩碼可以判斷兩臺(tái)主機(jī)是否中同一子網(wǎng)中。若兩臺(tái)主機(jī)的IP地址分別與它們的子網(wǎng)掩碼相“與”后的結(jié)果相同,則說(shuō)明這兩臺(tái)主機(jī)在同一子網(wǎng)中。
示例:
I P 地址  192.168.0.1
子網(wǎng)掩碼  255.255.255.0
轉(zhuǎn)化為二進(jìn)制進(jìn)行運(yùn)算:
I P 地址 11010000.10101000.00000000.00000001
子網(wǎng)掩碼 11111111.11111111.11111111.00000000
AND運(yùn)算
  11000000.10101000.00000000.00000000
轉(zhuǎn)化為十進(jìn)制后為:
  192.168.0.0

I P 地址  192.168.0.254
子網(wǎng)掩碼  255.255.255.0

轉(zhuǎn)化為二進(jìn)制進(jìn)行運(yùn)算:
I P 地址 11010000.10101000.00000000.11111110
子網(wǎng)掩碼 11111111.11111111.11111111.00000000
AND運(yùn)算
   11000000.10101000.00000000.00000000
轉(zhuǎn)化為十進(jìn)制后為:
   192.168.0.0
通過(guò)以上對(duì)兩臺(tái)計(jì)算機(jī)IP地址與子網(wǎng)掩碼的AND運(yùn)算后,我們可以看到它運(yùn)算結(jié)果是一樣的。均為192.168.0.0,所以這二臺(tái)計(jì)算機(jī)可視為是同一子網(wǎng)絡(luò)。
/*
* 功能: 判斷兩臺(tái)計(jì)算機(jī)IP地址是同一子網(wǎng)絡(luò)。
* 輸入?yún)?shù): String Mask: 子網(wǎng)掩碼,格式:“255.255.255.0”;
* String ip1: 計(jì)算機(jī)1的IP地址,格式:“192.168.0.254”;
* String ip2: 計(jì)算機(jī)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)
{
/*在這里實(shí)現(xiàn)功能*/
return 0;
}
輸入描述:

輸入子網(wǎng)掩碼、兩個(gè)ip地址
輸出描述:

得到計(jì)算結(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語(yǔ)言——如何判斷兩個(gè)IP在同一網(wǎng)段

ip_addr.h

#define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
                       (mask)->addr) == \
                       ((addr2)->addr & \
                       (mask)->addr))

在程序中,那個(gè)“\”表示它之前的程序和后面的是連接的,下一行和上一行是一個(gè)語(yǔ)句, 反斜杠符號(hào)起到長(zhǎng)代碼分行書(shū)寫(xiě)功能。

注意:C語(yǔ)言中的關(guān)鍵字不可以用“\”分行!

上述就是小編為大家分享的C語(yǔ)言中怎么判斷兩個(gè)IPv4地址是否屬于同一個(gè)子網(wǎng)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享文章:C語(yǔ)言中怎么判斷兩個(gè)IPv4地址是否屬于同一個(gè)子網(wǎng)
文章位置:http://bm7419.com/article36/giggpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)頁(yè)設(shè)計(jì)公司建站公司、企業(yè)網(wǎng)站制作域名注冊(cè)、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作