출처 주소
http://www.sockaddr.com/ExampleSourceCode.html
바로 받기
- 원본 코드
- 새로 친 코드
변경 내용 - 약간의 한글화
수행 내용
- WinSock의 gethostbyX() 함수로 호스트 이름이나 IP 주소로 hostent 구조체 를 받습니다.
- 수행화면
코드 보기
http://www.sockaddr.com/ExampleSourceCode.html
바로 받기
- 원본 코드
- 새로 친 코드
변경 내용 - 약간의 한글화
수행 내용
- WinSock의 gethostbyX() 함수로 호스트 이름이나 IP 주소로 hostent 구조체 를 받습니다.
- 수행화면
코드 보기
/* 저작권 공지 http://www.sockaddr.com/ExampleSourceCode.html http://cakel.tistory.com 교육용을 목적으로 자유롭게 사용이 가능합니다. hostinfo.cpp WinSock gethostbyX 함수로 호스트 이름이나 IP 주소로 hostent 구조체 를 받습니다. 서버 주소를 IP 주소를 명령줄에서 받습니다. 실행 예 : hostinfo.exe [IP 주소 또는 도메인 주소] 예제) hostinfo www.naver.com (엔터) HOSTENT ----------------- Host Name........: naver.com Host Aliases..... Address type.....: 2 (AF_INET) Address length...: 4 IP Addresses.....: 222.122.84.250 : 222.122.84.200 hostinfo cakel.tistory.com (엔터) HOSTENT ----------------- Host Name........: cakel.tistory.com Host Aliases..... Address type.....: 2 (AF_INET) Address length...: 4 IP Addresses.....: 211.172.252.15 */ #include <stdio.h> #include <winsock.h> #pragma comment(lib,"ws2_32.lib") // WinSock library 를 compile시 link 합니다. int Printhostent(LPCSTR lpServerNameOrAddress); int main(int argc, char* *argv) { // 필요한 WinSock Version은 1.1 입니다. WORD wVersionRequested = MAKEWORD(1,1); WSADATA wsaData; int nRC; // 인수를 검사합니다. if (argc != 2) { fprintf(stderr, "\n사용법 : hostinfo.exe [서버 주소 또는 IP주소]\n"); return 0; } // WinSock.dll 를 초기화합니다. nRC = WSAStartup(wVersionRequested, &wsaData); if (nRC) { fprintf(stderr, "\nWSAStartup() 오류 입니다 : %d\n", nRC); WSACleanup(); // 종료 합니다. return 0; } // WinSock 버전을 확인 합니다. if (wVersionRequested != wsaData.wVersion) { fprintf(stderr,"\nWinSock version 1.1 이 지원하지 않습니다.\n"); WSACleanup(); return 0; } // Printhostend() 함수로 작업을 합니다. nRC = Printhostent(argv[1]); if (nRC) fprintf(stderr, "\nPrinthostent 반환 코드 : %d\n", nRC); WSACleanup(); // WinSock Application 을 제거 합니다. return 0; } int Printhostent(LPCSTR lpServerNameOrAddress) { LPHOSTENT lpHostEntry; // 호스트 단위 구조체를 가리키는 포인터 입니다. struct in_addr iaHost; // 인터넷 주소 구조체 입니다. struct in_addr *pinAddr; // 인터넷 주소를 가리키는 포인터 입니다. LPSTR lpAlias; // 별명(2차 이름)을 가리키는 문자열 포인터 입니다. int iNdx; // inet_addr() 함수로 도메인 이름인지 아니면 주소인지 결정 합니다. iaHost.s_addr = inet_addr(lpServerNameOrAddress); if (iaHost.s_addr == INADDR_NONE) { // IP 주소가 아니였습니다, 이름으로 간주 합니다. lpHostEntry = gethostbyname(lpServerNameOrAddress); } else { // 유효한 IP 주소 문자열이라 확인 되었습니다. lpHostEntry = gethostbyaddr( (const char*) &iaHost, sizeof(struct in_addr), AF_INET); } // 되돌아 오는 값을 검사합니다. if(lpHostEntry == NULL) { fprintf(stderr, "\n호스트에서 문제가 생겼습니다 : %d", WSAGetLastError()); return WSAGetLastError(); } // 구조체를 출력합니다. printf("\n\nHOSTENT"); printf("---------------"); // 호스트 이름 printf("\n호스트 이름(Host Name) .......... : %s", lpHostEntry->h_name); // 호스트의 별명들의 목록 printf("\n\n호스트의 별명(aliase)............"); for(iNdx = 0; ; iNdx++) { lpAlias = lpHostEntry->h_aliases[iNdx]; if(lpAlias == NULL) break; printf(" : %s", lpAlias); printf("\n "); } // 주소 형식 printf("\n주소 형식 ....................... : %d", lpHostEntry->h_addrtype); if(lpHostEntry->h_addrtype == AF_INET) printf("(AF_INET)"); else printf("(알수 없는 형식)"); // 주소 길이 printf("\n주소 길이 ....................... : %d", lpHostEntry->h_length); // IP 주소의 목록 printf("\nIP 주소 ........"); for(iNdx = 0; ; iNdx++) { pinAddr = ( (LPIN_ADDR)lpHostEntry->h_addr_list[iNdx] ); if(pinAddr == NULL) break; printf(" %s", inet_ntoa(*pinAddr) ); printf("\n "); } printf("\n"); return 0;
Posted by 카켈