[완료]10진수로 된 ip주소를 변환할려고 합니다.
10진수 형태로 된 ip주소를 파일에서 불러와 점으로 찍힌 ip주소로 출력할려고 하는데 잘못된 주소가 출력이 되네요.
주소를 잘못 참조 하는건지;; 잘못된 점 지적 부탁바랍니다~
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define NAME_SIZE 50
#define BUFSIZE 30
#define LINE_SIZE 256
#define LINE_BUF 10000
char *trimLeft(char *string);
void ip_convert(char *p);
int main(void)
{
char ip_addr[NAME_SIZE];
char ip_addr1[NAME_SIZE];
char *a;
memset(ip_addr,'\0',NAME_SIZE);
const char del_line[] = "=\t";
int size;
char *token;
char file_buf[LINE_BUF];
FILE *fp;
if((fp = fopen("ipaddr.txt","r")) == NULL) // (ip_addr = 3555800683)파일에는 이렇되 있습니다.
{
printf("125 Can't read file\n");
exit(1);
}
while(!feof(fp))
{
memset(file_buf,'\0',LINE_BUF);
fgets(file_buf,LINE_BUF,fp);
if(strchr(file_buf,'#') == NULL)
{
if(strstr(file_buf,"ip_addr"))
{
strtok(file_buf,del_line);
token = strtok(NULL,del_line);
size = strlen(token);
strncpy(ip_addr,token,size);
strncpy(ip_addr,token,NAME_SIZE);
if(ip_addr[size-1] == '\n')
{
ip_addr[size-1] = '\0';
}
}
}
}
a = trimLeft(ip_addr);
ip_convert(a);
}
char *trimLeft(char *string)
{
while(*string)
{
if(isspace(*string))
{
++string;
}
else
{
break;
}
}
return string;
}
void ip_convert(char *p)
{
char *ul = "UL";
strcat(p,ul);
printf("%s\n",p);
struct in_addr in;
unsigned long int ip = (unsigned long)p;
in.s_addr = htonl(ip);
printf("%s\n",inet_ntoa(in));
}
참고하세요
분명히 텍스트에 저장된 데이터는 ip정보일테고..
그렇다면 대충 ip정보가 8bit씩 4덩이 일테고..
그렇다면 저 정보를 4덩이로 나워서 찍어보면 나오겠지요
3555800683 D3F1366B D3 F1 36
3555800683
D3F1366B
D3 F1 36 6B
211 241 54 107
어느 단계에서 오류가 나나요?
오류는 아니구요
컴파일 해서 실행할때 마다 매 다른 주소가 출력 됩니다.
그리고 htonl함수를 이용해서 16진수 변환과정을 볼 수 없는데 방법이 있는지요?;
그게 오류죠..
일정해야 하는 것이 일정치 않으면 그게 오류죠.. 어느 단계부터 엉뚱한 값이 끼어드는지요?
php source code 에서
php source code 에서 long2ip function 을 함번 참고해 보심이
ip_convert()가
ip_convert()가 이상하네요.
이건 문자열의 주소를 ip address로 쓰는 겁니다.
실행할 때마다 값이 달라지는 건 그 때문이고요.
실은 문자열의 내용을 읽어야죠.
아..
문자열 주소를 읽어드려서 그랬군요..
해결점을 고쳐주셔서 감사합니다~^^
팁을 드리자면.. 10진
팁을 드리자면.. 10진 ip address의 경우 Union 을 이용하시면 편하게 변환하실 수 있습니다.
안 돌려봤지만 되지 않을까 싶네요.. :)
--
Emerging the World!
Emerging the World!
댓글 달기