Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
一段C代码的功能需求,请大神支招!
deepin Talks
746
views ·
1
replies ·
To
floor
Go
zwq133
deepin
2018-09-28 03:00
Author
本帖最后由 zwq133 于 2018-9-28 07:35 编辑
#include
#include
#include
void main() {
char len = 0;
int a=0, b=0, c=0;
char wangduan[16], intranet[16];
printf("\n输入要测试的网段,不含地址:");
while (fgets(wangduan, 16, stdin)) {
len = strlen(wangduan);
if (wangduan[len - 1] == '\n') {
wangduan[len - 1] = '\0';
}
if (sscanf(wangduan, "%d.%d.%d", &a, &b, &c)
&& a >= 0 && a <= 255 && b >= 0 && b <= 255 && c >= 0 && c <= 255) {
sprintf(intranet, "%d.%d.%d", a, b, c);
printf("%s", intranet);
break;
}
else {
printf("不是正确的网段,请重新输入:");
}
}
int min=0, max=0;
printf("\n输入IP地址范围,用空格隔开:");
while (1) {
scanf("%d %d", &min, &max);
while (getchar()!='\n');
if (min <= 0 || min >= 255 || max <= 0 || max >= 255 || min >= max) {
printf("起始数不能大于或等于结束数:");
}
else {
printf("\n———————————————————\n");
break;
}
}
// 顺序查找在线主机(待实现并发查找功能)
int addr;
char hostip[17], chazhao[45];
for (addr = min; addr<=max; addr++) {
sprintf(hostip, "%s.%d", intranet, addr);
sprintf(chazhao, "ping -c1 -i0.2 %s > /dev/null", hostip);
int test = system(chazhao);
if (WEXITSTATUS(test) == 0) {
// 如果实现并发查找,则排序后输出(待排序)
printf("| %-16s|\n", hostip);
}
}
printf("———————————————————\n");
}
Copy the Code
恩,注释处就是功能需求了,新手表示不能实现,请大神些秀出你们的脑洞,谢谢!!
排版可能混乱,下方附上源码。
Reply
Like 0
Favorite
View the author
All Replies
wtz
deepin
2018-09-28 05:15
#1
主机在线检测:
https://blog.csdn.net/m0_37886429/article/details/78795100
排序:等查找结束之后,统一对输出内容进行排序即可(随便用什么排序算法都行)。
Reply
Like 0
View the author
Please
sign
in first
Featured Collection
Change
[Tutorial] deepin25 WSL Offline Installation Guide
UOS AI 2.8 Released! Three New Intelligent Agents & Major Evolution
Solid Q&A | deepin 25 Common Questions – The Immutable System Edition
New Thread
Popular Events
More
排版可能混乱,下方附上源码。