2021. 4. 22. 18:09 .Net 교육/Network Programming
[C++] 로컬 호스트 IP주소 얻어오기
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32") //윈속 사용 라이브러리
#pragma warning(disable : 4996)
//IPv4 주소 : 4바이트
int main()
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2)/*0x0202*/, &wsadata);
char hname[256];
gethostname(hname, 256);
printf("내 컴퓨터 이름 :%s\n", hname);
hostent* hent = gethostbyname(hname);
while(hent->h_name)
{
if (hent->h_addrtype == AF_INET)
{
IN_ADDR addr;
memcpy(&addr, hent->h_addr_list[0], sizeof(IN_ADDR));
printf("%s\n", inet_ntoa(addr));
}
hent++;
}
WSACleanup();
return 0;
}
'.Net 교육 > Network Programming' 카테고리의 다른 글
[C++] IPv4 주소 (0) | 2021.04.22 |
---|---|
[C++] WinSock 초기화 (0) | 2021.04.22 |