MFC에서 IP 주소 구하기(얻기,찾기)

내부 IP

//StdAfx.h에 선언
#include "winsock2.h"
#pragma comment(lib, "WS2_32.lib")
#define DESIRED_WINSOCK_VERSION        0x0101
#define MINIMUM_WINSOCK_VERSION        0x0001
//선언 끝
CString GetInIpAddress()
WSADATA wsadata;
CString strIP; // 이 변수에 IP주소가 저장된다.
strIP = "";
if( !WSAStartup( DESIRED_WINSOCK_VERSION, &wsadata ) )
  if( wsadata.wVersion >= MINIMUM_WINSOCK_VERSION )
    HOSTENT *p_host_info;
    IN_ADDR in;
    char host_name[128]={0, };
    gethostname(host_name, 128);
    p_host_info = gethostbyname( host_name );
   if( p_host_info != NULL )
     for( int i = 0; p_host_info->h_addr_list[i]; i++ )
      memcpy( &in, p_host_info->h_addr_list[i], 4 );
      strIP = inet_ntoa( in );
  return strIP;



외부 IP

CString GetIpAddress()
	WORD wVersionRequested;
	WSADATA wsaData;
	char name[255];
	PHOSTENT hostinfo;
	CString strIpAddress = _T("");
	wVersionRequested = MAKEWORD(2, 0);
	if(WSAStartup(wVersionRequested, &wsaData) == 0)
		if(gethostname(name, sizeof(name)) == 0)
			if((hostinfo = gethostbyname(name)) != NULL)
				strIpAddress = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
	return strIpAddress;


클라이언트 IP얻기

sockaddr 구조체

struct sockaddr{
sa_family_t sa_family;  //  PF_INET= IPv4 주소체계.
char sa_data[14];       // 사용하는 주소 정보.

sockaddr_in 구조체

IPv4 체계구조체다.
범용 주소 구조체로 sockaddr을 사용하지만, 주소체계에 따라 별도의 전용 구조체 사용 권장.
라이브러리에 넘길 때는 sockaddr로 형 변환을 하여 넘긴다.다른 주소체계 중 Local Unix 주소 체계는 sockaddr_un 구조체를 사용.

struct sockaddr_in{
    sin_family_t sin_family;    // IPv4 주소체계 AF_INET으로 설정
    unist16_t sin_port;         // 포트
    struct in_addr sin_addr;    // IP주소 정수 타입 구조체
    char sin_zero[8];           // sockaddr 크기 맞추기 위한 버퍼.
SOCKET client_s;
char lpszMsg[100];
SOCKADDR_IN m_clientAddr;
client_s = accept(s, (LPSOCKADDR)&m_clientAddr, &m_nClientAddrLen);
if (client_s == INVALID_SOCKET) {
        wsprintf(lpszMsg, "accept() 에러 번호: %d", WSAGetLastError());
        MessageBox(hwnd, lpszMsg, "에러", MB_OK);
} else {
        wsprintf(lpszMsg, "client IP: %s",inet_ntoa(m_clientAddr.sin_addr.s_addr)); MessageBox        (hwnd, lpszMsg, "에러", MB_OK);

“MFC IP얻기”의 10개의 댓글

  1. Hi there, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam
    remarks? If so how do you reduce it, any plugin or anything you can advise?

    I get so much lately it’s driving me mad so any help is very much appreciated.

  2. Do you have a spam issue on this site; I also am a blogger, and I was curious about your situation; many of us have created some nice methods and we are looking to swap techniques with other folks, please
    shoot me an email if interested.

  3. I’m really enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more pleasant for me to
    come here and visit more often. Did you hire out a developer to create your theme?

    Excellent work!

  4. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is
    needed to get setup? I’m assuming having a blog like yours would cost a pretty penny?

    I’m not very web savvy so I’m not 100% sure. Any tips or advice would be
    greatly appreciated. Many thanks

댓글 남기기