struct sockaddr 형 변수를 스트링으로 바꿔서 출력하고자 합니다. 감사합니다.
sprintf() 함수를 이용하세요. 간단하게 IP를 나타내는 부분은...
struct sockaddr_in abcd; ...
sprintf (buff, "%d.%d.%d.%d", abcd.sin_addr.s_addr & 0x00FF, abcd.sin_addr.s_addr >> 8 & 0x00FF, abcd.sin_addr.s_addr >> 16 & 0x00FF, abcd.sin_addr.s_addr >> 24 & 0x00FF);
형태로 하면 buff에 IP가 스트링 값으로 들어갑니다.
몇가지 주소 관련된 함수입니다. 저는 .cfg(ini형식으로) 형식으로 읽어 들여서 바꾸는데 아래 두 함수를 한번 이용해보세요. 스트링을 인터넷 구 조에 넣는것과 구조를 스트링으로 바꿔주는겁니다.
int string_to_addr(struct in_addr *addr, const char *str) { struct in_addr addr_t; struct hostent *hostent_t;
if(inet_pton(AF_INET, str, &addr_t)) { *addr = addr_t; return 0; }
if((hostent_t = gethostbyname(str)) == NULL) return -1;
if(hostent_t -> h_addrtype != AF_INET) return -1;
*addr = *((struct in_addr *) (hostent_t -> h_addr_list [0]));
return 0;
}
const char *addr_to_string(char *str, struct in_addr addr, size_t len) { return inet_ntop(AF_INET, &addr, str, len); }
즐포하세요. 웁~ 즐통~
텍스트 포맷에 대한 자세한 정보
<code>
<blockcode>
<apache>
<applescript>
<autoconf>
<awk>
<bash>
<c>
<cpp>
<css>
<diff>
<drupal5>
<drupal6>
<gdb>
<html>
<html5>
<java>
<javascript>
<ldif>
<lua>
<make>
<mysql>
<perl>
<perl6>
<php>
<pgsql>
<proftpd>
<python>
<reg>
<spec>
<ruby>
<foo>
[foo]
Re: struct sockaddr 스트링으로 바꾸기
sprintf() 함수를 이용하세요.
간단하게 IP를 나타내는 부분은...
struct sockaddr_in abcd;
...
sprintf (buff, "%d.%d.%d.%d",
abcd.sin_addr.s_addr & 0x00FF,
abcd.sin_addr.s_addr >> 8 & 0x00FF,
abcd.sin_addr.s_addr >> 16 & 0x00FF,
abcd.sin_addr.s_addr >> 24 & 0x00FF);
형태로 하면 buff에 IP가 스트링 값으로 들어갑니다.
Re: struct sockaddr 스트링으로 바꾸기
몇가지 주소 관련된 함수입니다. 저는 .cfg(ini형식으로) 형식으로 읽어
들여서 바꾸는데 아래 두 함수를 한번 이용해보세요. 스트링을 인터넷 구
조에 넣는것과 구조를 스트링으로 바꿔주는겁니다.
int string_to_addr(struct in_addr *addr, const char *str)
{
struct in_addr addr_t;
struct hostent *hostent_t;
if(inet_pton(AF_INET, str, &addr_t)) {
*addr = addr_t;
return 0;
}
if((hostent_t = gethostbyname(str)) == NULL)
return -1;
if(hostent_t -> h_addrtype != AF_INET)
return -1;
*addr = *((struct in_addr *) (hostent_t -> h_addr_list
[0]));
return 0;
}
const char *addr_to_string(char *str, struct in_addr addr, size_t
len) {
return inet_ntop(AF_INET, &addr, str, len);
}
즐포하세요. 웁~ 즐통~
댓글 달기