gpt4 book ai didi

c++ - 增加IP地址

转载 作者:可可西里 更新时间:2023-11-01 12:42:18 25 4
gpt4 key购买 nike

在该程序中,我想增加 IP 地址。我看到这样的输出:

125.23.45.67
126.23.45.67
127.23.45.67
128.23.45.67
129.23.45.67
130.23.45.67
131.23.45.67
132.23.45.67
133.23.45.67
134.23.45.67

但我想看到这样的输出:

124.23.45.67
124.23.45.68
124.23.45.68
124.23.45.70
124.23.45.71
124.23.45.72
124.23.45.73
124.23.45.74
124.23.45.75
124.23.45.76

程序代码如下:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#include "winsock2.h"
#pragma comment(lib,"wsock32.lib")

void main()
{
in_addr adr1;
in_addr adr2;
int i;

adr1.s_addr=inet_addr("124.23.45.67");
adr2.s_addr=inet_addr("as.34.34.56");
if (adr1.s_addr!=INADDR_NONE)
cout << " adr1 correct" << endl;
else
cout << " adr1 incorect " << endl;

if (adr2.s_addr!=INADDR_NONE)
cout << " adr2 correct" << endl;
else
cout << " adr2 incorect" << endl;

cout << inet_ntoa(adr1) << endl;
cout << inet_ntoa(adr2) << endl;

for (i=0;i<10;i++)
{
adr1.s_addr ++;
cout << inet_ntoa(adr1) << endl;
}
}

最佳答案

大端和小端得到另一个!使用 htonl 和 ntohl 来回转换。

for (i=0;i<10;i++)
{
adr1.s_addr = htonl(ntohl(adr1.s_addr) + 1);

cout << inet_ntoa(adr1) << endl;
}

关于c++ - 增加IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6743438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com