bcb 平台 根据电脑名获得ip 根据ip获得电脑

因工作需要,根据客户电脑,我要把他的ip 保存下来 
我写了个类,做了个封装


//---------------------------------------------------------------------------

#ifndef GetComputeIPH
#define GetComputeIPH
#include <winsock2.h>
#include <Classes.hpp>

//---------------------------------------------------------------------------
class TGetComputeIP
{
  private:
        int initSocketFlag;  //初始化是否成功
  public:
     __fastcall TGetComputeIP();
     __fastcall ~TGetComputeIP();
     String __fastcall GetComputeIP(String HostName);
     String __fastcall GetComputeName(String IP);

};
#endif

//--------------------------code---------------------------------------------------------
//---------------------------------------------------------------------------


#pragma hdrstop

#include "GetComputeIP.h"
//---------------------------------------------------------------------------
__fastcall TGetComputeIP::TGetComputeIP()
{
    initSocketFlag = 0;

    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
 
    wVersionRequested = MAKEWORD( 2, 0 );
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 )
    {
      initSocketFlag = -1;
      MessageBox(NULL,"not found winsock2 dll","消息",MB_OK);
    }
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 0 )
    {
      initSocketFlag = -2;
      WSACleanup( );
    }


}
__fastcall TGetComputeIP::~TGetComputeIP()
{
        if(initSocketFlag == 0)
        {
                WSACleanup();
        }
}
String __fastcall TGetComputeIP::GetComputeIP(String HostName)
{
        String IP;
        if(initSocketFlag == 0)
        {
            PHOSTENT   hostinfo;
            if((hostinfo = gethostbyname(HostName.c_str())) != NULL)
            {
                 IP = inet_ntoa(*(struct   in_addr   *)*hostinfo->h_addr_list);
            }

        }
        return IP;
}
String  __fastcall TGetComputeIP::GetComputeName(String IP)
{
  String ComputeName;
  if(initSocketFlag == 0)
  {
    DWORD dwIPAddr ;
    dwIPAddr = inet_addr(IP.c_str());
    if(dwIPAddr !=  INADDR_NONE)      //无效得地址
    {
        PHOSTENT   hostinfo;
        if((hostinfo = gethostbyaddr((LPSTR)&dwIPAddr, 4, AF_INET)) != NULL)
        {
             ComputeName = hostinfo->h_name;
        }
    }
  }
  return  ComputeName;
}
#pragma package(smart_init)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值