#include <iostream>
#include"stdafx.h"
#include"winsock2.h"
#include<tchar.h>
#include <string>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[]) {
if (argc != 3) {
printf("Usage: CalculateSubnet netaddr netmask\r\nExample:CalculateSubnet 192.168.0.0 255.255.255.0");
return 1;
}
bool IsValidIP(char* ip);{
std::string sip = ip;
int pos = (int)sip.find_first_of(".");
if (pos == 0)return false;
std::string s1 = sip.substr(0, pos);
sip = sip.substr(pos + 1, sip.length() - pos);
if (s1.length() > 3) return false;
for (int i = 0; i < (int)s1.length(); i++) {
int c = s1.c_str()[i];
if (!isdigit(c))return false;
}
int a = atoi(s1.c_str());
if (a < 1 || a>255)return false;
pos = (int)sip.find_first_of(".");
std::string s2 = sip.substr(0, pos);
sip = sip.substr(pos + 1, sip.length() - pos);
if (s2.length() > 3) return false;
for (int i = 0; i < (int)s2.length(); i++) {
int c = s2.c_str()[i];
if (!isdigit(c))return false;
}
a = atoi(s2.c_str());
if (a > 255)return false;
pos = (int)sip.find_first_of(".");
std::string s3 = sip.substr(0, pos);
sip = sip.substr(pos + 1, sip.length() - pos);
if (s3.length() > 3) return false;
for (int i = 0; i < (int)s3.length(); i++) {
int c = s3.c_str()[i];
if (!isdigit(c))return false;
}
a = atoi(s3.c_str());
if (a > 255)return false;
pos = (int)sip.find_first_of(".");
std::string s4 = sip.substr(0, pos);
sip = sip.substr(pos + 1, sip.length() - pos);
if (s4.length() > 3) return false;
for (int i = 0; i < (int)s4.length(); i++) {
int c = s4.c_str()[i];
if (!isdigit(c))return false;
}
a = atoi(s4.c_str());
if (a > 254)return false;
return true;
}
if (!IsValidIP(argv[1])) {
printf("%s is not a valid ip.\n", argv[1]);
return 1;
}
if (!IsValidIP(argv[2])) {
printf("%s is not a valid ip.\n", argv[2]);
return 1;
}
printf("netaddr:%s\n", argv[1]);
printf("netmask:%s\n", argv[2]);
unsigned long lnetaddr = ntohl(inet_addr(argv[1]));
unsigned long lnetmask = ntohl(inet_addr(argv[2]));
unsigned long l_first_netaddr = lnetaddr & lnetmask;
unsigned long l_broadcast = lnetaddr | ~lnetmask;
long num = l_broadcast - l_first_netaddr - 1;
printf("Number of valid IPs:%d\n\n", num);
printf("IPs in subnet:\n===============\n");
for (unsigned long i = l_first_netaddr + 1; i < l_broadcast; i++) {
in_addr IPAddr;
IPAddr.S_un.S_addr = ntohl(i);
printf("%s\n", inet_ntoa(IPAddr));
}
报错一直运行不出来,求解
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- threenewbee 2023-10-23 22:48关注
bool IsValidIP(char* ip);
你这是定义还是调用???
整个语法都不通本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
- ¥15 孟德尔随机化怎样画共定位分析图
- ¥18 模拟电路问题解答有偿速度
- ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
- ¥15 误删注册表文件致win10无法开启
- ¥15 请问在阿里云服务器中怎么利用数据库制作网站
- ¥60 ESP32怎么烧录自启动程序
- ¥50 html2canvas超出滚动条不显示
- ¥15 java业务性能问题求解(sql,业务设计相关)
- ¥15 52810 尾椎c三个a 写蓝牙地址