C gethostname() 호스트 이름 구하기 함수
호스트 이름을 구합니다.
- 헤더: unistd.h
- 형태: int gethostname(char *name, size_t len)
- 인수: char *name 이름을 받을 문자열 버퍼
size_t len 문자열 버퍼 크기 - 반환: 0 == 호스트 이름 구하기 성공
-1 == 실패, errno는적당한 값으로 설정된다.
C언어 gethostname() 함수 예제
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main( void)
{
char host_name[80];
struct hostent *host_entry;
int ndx;
if ( 0 != gethostname( host_name, sizeof( host_name))){
printf( "gethostname() 실행 실패\n");
exit( 1);
}
printf( "%s\n", host_name);
host_entry = gethostbyname( host_name);
if ( !host_entry){
printf( "gethostbyname() 실행 실패\n");
exit( 1);
}
for ( ndx = 0; NULL != host_entry->h_addr_list[ndx]; ndx++)
printf( "%s\n", inet_ntoa( *(struct in_addr*)host_entry->h_addr_list[ndx]));
return 0;
}
C언어 gethostname() 예제 실행 결과
]$ ./a.out
jwMint
127.0.1.1
]$
'컴퓨터 > 프로그래밍' 카테고리의 다른 글
C언어 도메인 이름으로 hostent 정보 구하기 함수 gethostbyname() (0) | 2020.03.16 |
---|---|
C언어 소켓으로부터 자료 수신 함수 recvfrom() (0) | 2020.03.16 |
C언어 소켓으로 데이터를 전송 함수 sendto() (2) | 2020.03.16 |