728x90
๋ฌธ์์ด ํ์์ ๋๋ฉ์ธ -> 4๋ฐ์ดํธ ๋คํธ์ํฌ ๋ฐ์ดํธ ์์๋ก๋ ์ด์ง ํ์์ ์ฃผ์
ํจ์
#include <netdb.h>
struct hostent * gethostbyname(const char *name);
๋ฐํ ๊ฐ
- ์ฑ๊ณต ์ : hostent ๊ตฌ์กฐ์ฒด ์ฃผ์
- ์คํจ ์ : NULL
์ธ์
- name : ๋๋ฉ์ธ ์ด๋ฆ
์ธ์ name์ด ๊ฐ๋ฅดํค๋ ๋๋ฉ์ธ ์ด๋ฆ์ ํด๋นํ๋ ์๋ฒ์ ์ ๋ณด๋ฅผ ๋คํธ์ํฌ์์์ ๊ฒ์ํด์ ๊ตฌ์กฐ์ฒด hostent์ ๋ฃ์ด ์ค๋ค.
ํจ์
#include <netdb.h>
struct hostent * gethostnbyaddr(const char *addr, int len, int type);
๋ฐํ ๊ฐ
- ์ฑ๊ณต ์ : hostent ๊ตฌ์กฐ์ฒด ์ฃผ์
- ์คํจ ์ : NULL
์ธ์
- addr : 32๋นํธ ์ด์ง ๊ฐ์ผ IP ์ฃผ์
len : ์ฃผ์ ๊ธธ์ด
type : ์ฃผ์ ์ ํ
๊ตฌ์กฐ์ฒด
struct hostent {
char *h_name; // ๋๋ฉ์ธ ์ด๋ฆ
char **h_aliases; // ๋ณ๋ช
๋ค
int h_addrtype; // ์ฃผ์ ์ ํ
int h_length; // ์ฃผ์ ๊ธธ์ด
char **h_addr_list; // ๋คํธ์ํฌ ๋ฐ์ดํธ ์์๋ก ๋ ์ด์ง ํ์์ IP ์ฃผ์
}
#include<stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main() {
struct in_addr addr;
struct hostent *host;
const char *hostName = "www.eluon.com";
int i;
if((host = gethostbyname(hostName)) == NULL ) {
printf("gethostbyname() error - check network\n");
exit(-1);
}
printf("official name = %s \n", host->h_name);
for(i=0; host->h_aliases[i]; i++)
printf("aliases %d : %s \n", i+1, host->h_aliases[i]);
printf("address type = %d\n", host->h_addrtype);
printf("address length = %d\n", host->h_length);
i =0;
while(host->h_addr_list[i] != NULL) {
memcpy(&addr.s_addr, host->h_addr_list[i], 4);
printf("address = %s(0x%x)\n", inet_ntoa(addr), ntohl(*(long*)host->h_addr_list[i]));
i++;
}
}
๊ฒฐ๊ณผํ๋ฉด
'TIL (Today I Learned) > Network' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Network] HTTP/2 ์ง์์ฌ๋ถ ํ๋จ C๋ก ๊ตฌํ (0) | 2021.09.13 |
---|---|
[TCP/IP] select() (0) | 2021.09.11 |
[TCP/IP] ํฌํธ(Port) (0) | 2021.09.11 |
[TCP/IP] socketpair ํจ์๋ฅผ ์ด์ฉํ ์์ผ ํ๋ก๊ทธ๋จ์ ๊ตฌํ - ํ๋ก์ธ์ค ํ๋ (0) | 2021.09.11 |
[TCP/IP] ํ์ผ ๋ชฉ๋ก์ ์ถ๋ ฅํ๋ ์์ผ ํ๋ก๊ทธ๋จ ๊ตฌํ (0) | 2021.09.11 |