Yes, sorry. I've tried to simplify working in different modules now.
First one should export to dns_responden.txt those DNS servers. Working fine so far:
Code:
void extraerDNS() {
FILE* responsesdns = fopen("dns_responden.txt", "w");
FILE* dns = _popen(str, "r");
while (fgets(line, sizeof line, dns))
{
unsigned a = 0, b = 0, c = 0, d = 0;
unsigned digit = strcspn(line, "0123456789");
if (line[digit]
&& sscanf(line + digit, "%u.%u.%u.%u", &a, &b, &c, &d) == 4
&& a < 256 && b < 256 && c < 256 && d < 256)
{
char ip_str[20];
sprintf(ip_str, "%u.%u.%u.%u", a, b, c, d);
fprintf(responsesdns, "%s\n", ip_str);
}
}
_pclose(dns);
}
dns_responden.txt looks like this, format-wise (fictional DNS servers):
Code:
80.51.41.651
80.08.21.229
Now, back to my main, the backbone of my program, where I had implemented previous help to ping through different IP's (also from a redirected .txt file, namely ips_responden.txt):
Code:
FILE* responses = fopen("ips_responden.txt", "w");
while (fgets(ch, sizeof(ch), fp))
{
printf("\n Lanzando ping a %s", ch);
sprintf(ping_cmd, "ping %s", ch);
FILE* f = _popen(ping_cmd, "r");
while (fgets(line, sizeof line, f)) {
if (strstr(line, term)) {
fprintf(responses, "%s", ch);
}
}
fclose(f);
}
fclose(responses);
fclose(fp);
int best_response = 0;
char best_ip[sizeof(ipresp)] = "";
int best_valid = 0;
responses = fopen("ips_responden.txt", "r");
if (responses != NULL)
{
while (fgets(ipresp, sizeof(ipresp), responses))
{
int response1 = velocidad_media(ipresp);
if (!best_valid || response1 < best_response) {
best_response = response1;
strcpy(best_ip, ipresp);
best_valid = 1;
}
}
printf("\n\n Velocidad de respuesta media: %d ms.\n", best_response);
printf("\n IP seleccionada: %s", best_ip);
}
extraerDNS();
This came in hefty to compare among IP's, keeping the lower value as best_response and its corresponding IP as best_ip. The latter phase of my program involves almost the same, but considerably shorter since I don't need to ping before in order to select among those which respond.
As you can see, this final line invoked this new module, i.e. redirects the DNS servers to the .txt file.
I'm implementing the following block immediately afterwards just to be able to compare between them, too, and be able to print the "winner" as well. First off, I'll open the redirected .txt to be able to do the rest:
Code:
FILE* responsesdns = fopen("dns_responden.txt", "r");
int best_response_dns = 0;
char best_dns[sizeof(ip_str)] = "";
int best_valid_dns = 0;
if (responsesdns != NULL)
{
while (fgets(ip_str, sizeof(ip_str), responsesdns))
{
int dns1 = velocidad_media(ip_str);
if (!best_valid_dns || dns1 < best_response_dns) {
best_response_dns = dns1;
strcpy(best_dns, ip_str);
best_valid_dns = 1;
}
}
printf("\n\n Velocidad de respuesta media: %d ms.\n", best_response_dns);
printf("\n DNS seleccionada: %s", best_dns);
}
... but it doesn't print a thing. In fact, I'm getting just a "0" for best_response_dns and a null value for best_dns. I don't know how to fix it.
Does this make more sense now?
Please forgive my sloppy approach. As you can see, I'm just learning the basics and it's too frustrating as well. And your help is much appreciated, mind you.