C/C++ code#include
#include
#include
#include
#include
/*
* const char *inet_ntop(int af, const void *src,
* char *dst, socklen_t size);
*
*/
int make_random_ip(FILE *out, int num) {
int i, rd;
struct in_addr in;
char str_ip[20];
const char *ret = NULL;
if (out == NULL || num < 1) {
return -1;
}
srand((unsigned int)time(NULL));
for (i = 0; i < num; ) {
rd = rand();
in = *(struct in_addr*)&rd;
ret = inet_ntop(AF_INET, (const void*)&in, str_ip, sizeof(str_ip));
if (ret) {
if (fprintf(out, "%s ", ret) < 0) {
return -1;
}
++ i;
}
}
return 0;
}
int main(int argc, char* const argv[]) {
FILE *out = NULL;
if ((out = fopen("./random_ip.txt", "w+")) == NULL) {
fprintf(stderr, "[ERROR] %s\n", __FUNCTION__);
exit(1);
}
if (!make_random_ip(out, 10)) {
fseek(out, SEEK_SET, 0);
char str_ip[20];
while (fscanf(out, "%s", str_ip) == 1) {
printf("%s\n", str_ip);
}
}
return 0;
}