MFC获取本机IP


void CDemoDlg::OnTest()

{
 //初始WinSock
 WSADATA WSAData;
 if (WSAStartup(MAKEWORD(2,0), &WSAData) != 0)
 {
  return;
 }
 
 //本机名称获取
 CString strName = _T("");
 gethostname(strName.GetBuffer(1024), 1024);
 strName.ReleaseBuffer();
 //本机信息获取
 struct hostent* pHostEnt = gethostbyname(strName);
 if (pHostEnt == NULL)
 {
  return;
 }
 //本机IP地址获取
 CString strText = _T("");
 strText += _T("本机IP地址是:\n");
 int n = 0;
 while (pHostEnt->h_addr_list[n] != NULL)
 {
  CString strTemp = _T("");
  strTemp.Format(_T("%d.%d.%d.%d\n"),
  (pHostEnt->h_addr_list[n][0] & 0x00FF),
  (pHostEnt->h_addr_list[n][1] & 0x00FF),
  (pHostEnt->h_addr_list[n][2] & 0x00FF),
  (pHostEnt->h_addr_list[n][3] & 0x00FF));
  strText += strTemp;
  n++;
 }
 AfxMessageBox(strText);
 //清理WinSock
 WSACleanup(); 

}


---------------------------------------

首先输入:
#include "winsock.h"

      WORD wVersionRequested;
      WSADATA wsaData;
      char name[255];
      CString 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( );
      }
   AfxMessageBox(ip);

===========================================================

   知识点:
   1.IP获取,通过文件winsock.h,在模块中包含ws2_32.lib
   2.检查socket版本的函数,WSASTARTUP();
   3.获取主机名的函数,gethostname();
   4.gethostbyname(hostname)函数,返回hostent结构体类型信息,解析此结构体,可获得相关hostname的信息.本人认为用处不太大
   .
   5.根据hostname,通过inet_ntoa()函数转换成ip地址,其参数要用到sockaddr_in.以及memcpy函数.
   6.终止 Windows sockets API的函数WSAClearnup()函数

WSAGetLastError()函数用来获取winsock错误.
gethostname返回整形0,表示成功.否则失败.

==============================================================

给你一个完整的类(CSockInfo,加入你的工程,然后调用就可以了。
以后程序中需要的时候,把这两个文件加进去
在需要取IP的程序里
#include "sockinfo.h"

CSockInfo::GetLocalHostIP();

/
// SockInfo.h: interface for the CSockInfo class.
// Big Lee 2002
//

#if !defined(AFX_SOCKINFO_H__FA14210C_2BED_11D6_81EB_0080C8ED46AF__INCLUDED_)
#define AFX_SOCKINFO_H__FA14210C_2BED_11D6_81EB_0080C8ED46AF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CSockInfo 
{
public:
static CString GetLocalHostIP();
static CString GetLocalHostName();
CSockInfo();
virtual ~CSockInfo();

};

#endif // !defined(AFX_SOCKINFO_H__FA14210C_2BED_11D6_81EB_0080C8ED46AF__INCLUDED_)

///
// SockInfo.cpp: implementation of the CSockInfo class.
// Big Lee 2002 
//

#include "stdafx.h"

#include "SockInfo.h"

#include <afxsock.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

CSockInfo::CSockInfo()
{

}

CSockInfo::~CSockInfo()
{

}

CString CSockInfo::GetLocalHostName()
{
WORD wVersionRequested;
    WSADATA wsaData;
    char name[255];
memset(name,'\0',255);
    wVersionRequested = MAKEWORD( 2, 0 );

    if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
    {
if( gethostname (name, sizeof(name)) == 0)
        {
return name;
        }    
        WSACleanup( );
}
return "";
}

CString CSockInfo::GetLocalHostIP()
{
CString strIP("");
WORD wVersionRequested;
    WSADATA wsaData;
    char name[255];
memset(name,'\0',255);
    PHOSTENT hostinfo;
    wVersionRequested = MAKEWORD( 2, 0 );

    if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
    {
if( gethostname ( name, sizeof(name)) == 0)
        {
if((hostinfo = gethostbyname(name)) != NULL)
            {
strIP= inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
            }
        }    
        WSACleanup( );
}
return strIP;
}

转载于:https://my.oschina.net/ypimgt/blog/105594

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值