개발이야기/MFC
로컬 IP주소 얻기
coinplace
2012. 5. 17. 15:19
로컬 IP주소를 얻을 수 있는 함수입니다.
CString 타입으로 _T("xxx.xxx.xxx.xxx")와 같이 반환합니다.
#include <afxsock.h> CString GetIPAddress() { WORD wVersionRequested; WSADATA wsaData; char name[255]; CString ip; // 여기에 lcoal ip가 저장됩니다. PHOSTENT hostinfo; wVersionRequested = MAKEWORD( 2, 0 ); if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) { if( gethostname ( name, sizeof(name)) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); } } WSACleanup( ); } return ip; }