DNSサーバーの動作をシミュレートしようとしています。DNSサーバーには、次のような名前のDBがありhosts.txt
ますmachine_names/IP_addresses
。
equipo_00/169.0.1.169
sala_oeste_01/200.1.2.200
sala_oeste_02/200.2.3.200
sala_oeste_03/200.3.4.200
MEMFIS_04/201.4.5.201
ICARO_05/200.5.6.200
equipo_06/169.6.7.169
sala_este_07/201.7.8.201
sala_este_08/201.8.9.201
CEC_09/189.9.10.189
これが私のコードです:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
char* machine, *ip_add;
FILE* db;
char* flag;
char tmp[256];
char par[256];
printf("> ");
scanf("%s", par);
db = fopen("hosts.txt", "r");
while(db != NULL && fgets(tmp, sizeof(tmp), db) != NULL)
{
if(strstr(tmp, par))
{
flag = "1"; // flag setting to 1 means find the line
machine = strtok(tmp, "/");//here's supposed to get the value of the machine
ip_add = strtok(NULL, "/");//here's supposed to get the value of the ip adress
}
}//while
if(strcmp(flag, "1") == 0) //
{
printf("here\n");
}
else
{
flag = "0"; //flag setting to 0
printf("there\n");
}
printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add);
return 0;
}
私のコードは、マシンの値である標準入力から読み取り、サーバー上のそのマシンに割り当てられているIPアドレスを示します。問題は、プログラムが正常な動作ではないことです。コードを期待される出力に変更する方法がわかりません。
input: equipo_00
output: flag = 1 pc = equipo_00 server=169.0.1.169
質問がかなりばかげている場合は申し訳ありませんが、私はこの言語に不慣れです..あなたの助けに感謝し、私の英語を許します