unix c 소켓 통신에서 ip대신 도메인명을 사용하려고 하는데요.
글쓴이: jee89 / 작성시간: 수, 2006/09/06 - 8:37오후
안녕하세요.
unix c환경입니다.
아래와 같이 현재 통신하려는 상대방 주소에 ip를 대입하여서 사용하고 있습니다.
이것을 ip대신에 도메인명을 써서 세팅하는법이 있으면 힌트주시면 감사하겠습니다.
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons( PPort );
dest_addr.sin_addr.s_addr = inet_addr( PServerIP );
Result = connectInTime( sockfd, (struct sockaddr*)&dest_addr, sizeof(struct sockaddr), 60);
if( Result == -1 ) {
SG_CLOSE( sockfd );
return FALSE;
}
Forums:
gethostbyname() 이었던가..
오래되서 기억은 잘 안나지만..
아마.. gethostbyname() / gethostbyaddr()의 짝이었던거 같은데..
Man 페이지 참조하시고.. 더 자세한건. Unix Network Programming - Stevens.
The gethostbyname() function returns a structure of type hostent for the given host name. Here name is either a host name, or an IPv4 address in standard dot notation, or an IPv6 address in colon (and possibly dot) notation. (See RFC 1884 for the description of IPv6 addresses.) If name is an IPv4 or IPv6 address, no lookup is performed and gethostbyname() simply copies name into the h_name field and its struct in_addr equivalent into the h_addr_list[0] field of the returned hostent structure. If name doesn't end in a dot and the environment variable HOSTALIASES is set, the alias file pointed to by HOSTALIASES will first be searched for name (see hostname(7) for the file format). The current domain and its parents are searched unless name ends in a dot.
The gethostbyaddr() function returns a structure of type hostent for the given host address addr of length len and address type type. Valid address types are AF_INET and AF_INET6. The host address argument is a pointer to a struct of a type depending on the address type, for example a struct in_addr * (probably obtained via a call to inet_addr()) for address type AF_INET.
바람과 같이
gethostbyname() 우선
gethostbyname()
우선 /etc/hosts 파일 참조해서 ip 얻어올 겁니다. 없으면 DNS에서 얻어 오나? ㅎㅎ 기억 가물 가물 이군요.
static int
에러 처리가 들어가지는 않았지만.. 대충 이런식으로...
댓글 달기