C++Builder 6 获取本机IP与公网IP

 代码如下:

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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "urlmon.h"   //syradd
#include "stdio.h"    //syradd
#include "winsock2.h" //syradd
#include <wchar.h>

#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\urlmon.lib"   //syradd
#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\ws2_32.lib"   //syradd
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{//获取公网IP
    char Buf_File_Read[MAX_PATH] = {0};
    char IP_Temp[128];
    char IP_Host[64];
    int Result_URLDownloadToFile = 1;
    
    Result_URLDownloadToFile = URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp","IPinTheWorld.ini",0,NULL);
	if(Result_URLDownloadToFile != 0)//若URLDownloadToFile()成功,返回0
    {
    	MessageBox(NULL, "网络故障,无法接入Internet,请联系网络管理员","提示",MB_ICONASTERISK);
        return ;
    }
    
    FILE *fp_Download_URL_File=NULL;
    if ((fp_Download_URL_File=fopen( "IPinTheWorld.ini","r")) == NULL)
    {
        MessageBox(NULL, "系统故障,无法打开IP文件,请检查防火墙设置或联系管理人员","提示",MB_ICONASTERISK);
        return ;
    }

    if ( fp_Download_URL_File != NULL )
    {
        fseek(fp_Download_URL_File, 0, SEEK_SET);
        fread(Buf_File_Read,1,256,fp_Download_URL_File);
        fclose(fp_Download_URL_File);
        char* Start_IP = strstr(Buf_File_Read,"[");
        if (Start_IP)
        {
            sprintf( IP_Temp,"%s",Start_IP);
            int Buf_Length = strlen(IP_Temp);
            for (int i =0; i<Buf_Length; i++)
            {
                IP_Host[i] = IP_Temp[i+1];
                if (IP_Temp[i] == ']')
                {
                    IP_Host[i-1] = '\0';
                    DeleteFile("IPinTheWorld.ini");
                    continue;
                }

            }
        }

    }
    AnsiString IP_World = IP_Host;
    Memo1->Text = Memo1->Text + "\r\n公网IP:" + IP_World;
    //MessageBox(NULL, chIP,chIP,0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{//获取本机IP
    WSADATA Data_WSA;
    int Result_WSAStartup = 1;

	hostent *P_Host = NULL;
    char Computer_Name[128] = "";
    Result_WSAStartup = WSAStartup(MAKEWORD(2, 2), &Data_WSA);
    if (Result_WSAStartup != 0)//无错返回0
    {
        MessageBox(NULL, "本机故障,无法启动网络服务,请联系网络管理员","提示",MB_ICONASTERISK);
        return ;
    }
    gethostname(Computer_Name, 128);//获取指定计算机的名字
    if(Computer_Name[0] == '\0')
    {
        MessageBox(NULL, "本机网络故障,无法接入局域网,请联系网络管理员","提示",MB_ICONASTERISK);
        return ;
    }
    //Memo1->Text = s;
    P_Host = gethostbyname(Computer_Name);
    if(P_Host == NULL)
    {
        //Memo1->Text = "p NULL ";
        MessageBox(NULL, "本机网络故障,无法获取IP,请联系管理员","提示",MB_ICONASTERISK);
        return ;
    }
    AnsiString IP_Host = inet_ntoa(*((in_addr *)P_Host->h_addr));//获取指定计算机的IP地址

    if(P_Host != NULL)
        {delete  P_Host;}
    P_Host = NULL; 
    WSACleanup();//完成对WSAStartup()的调用
    Memo1->Text = Memo1->Text + "\r\n局域网IP:" + IP_Host;
    //MessageBox(NULL, HostIP,HostIP,0);
}
//---------------------------------------------------------------------------


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值