溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-07-02 17:17:51 來源:億速云 閱讀:623 作者:Leah 欄目:編程語言

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

問題描述:

現(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地址,然后使用與(&)運(yùn)算,分別將兩個IP地址與掩碼相與,若最后的值相同,則為同一個子網(wǎng),否則不是。

以下函數(shù)的作用是將字符串格式的IP轉(zhuǎn)化為4字節(jié)的IP(因?yàn)槭?字節(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ù)字,實(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)掩碼是用來判斷任意兩臺計(jì)算機(jī)的IP地址是否屬于同一子網(wǎng)絡(luò)的根據(jù)。
子網(wǎng)掩碼與IP地址結(jié)構(gòu)相同,是32位二進(jìn)制數(shù),其中網(wǎng)絡(luò)號部分全為“1”和主機(jī)號部分全為“0”。利用子網(wǎng)掩碼可以判斷兩臺主機(jī)是否中同一子網(wǎng)中。若兩臺主機(jī)的IP地址分別與它們的子網(wǎng)掩碼相“與”后的結(jié)果相同,則說明這兩臺主機(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
通過以上對兩臺計(jì)算機(jī)IP地址與子網(wǎng)掩碼的AND運(yùn)算后,我們可以看到它運(yùn)算結(jié)果是一樣的。均為192.168.0.0,所以這二臺計(jì)算機(jī)可視為是同一子網(wǎng)絡(luò)。
/*
* 功能: 判斷兩臺計(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)掩碼、兩個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語言——如何判斷兩個IP在同一網(wǎng)段

ip_addr.h

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

在程序中,那個“\”表示它之前的程序和后面的是連接的,下一行和上一行是一個語句, 反斜杠符號起到長代碼分行書寫功能。

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

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI