⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipextra.pas

📁 IP地址查询,可以在互联网上搜索不同的IP地址为网络编程提供个接例程
💻 PAS
字号:
unit IPExtra;

interface

uses sysutils, winsock, windows;

var tcpip_ready: boolean;

const
  INVALID_IP_ADDRESS= $ffffffff;  {only invalid as a host ip, maybe OK for broadcast}

function Local_hostname:string;
function Local_ip_address:longint;
function internet_date(date: TDateTime):string;
function lookup_hostname(const hostname:string):longint;
function ip2string(ip_address:longint):string;
function resolve_hostname(ip: longint):string;

implementation

//------------------------------------------
//LocalHost Name
function Local_hostname:string;
const bufsize=255;
var buf: pointer;
    RemoteHost : PHostEnt;
begin
     buf:=nil;
     result:='';
     try
        getmem(buf,bufsize);
        winsock.gethostname(buf,bufsize);   //this one maybe without domain
        RemoteHost:=Winsock.GetHostByName(buf);
        result:=pchar(RemoteHost^.h_name);
     finally
        if buf<>NIL then  freemem(buf,bufsize);
     end;
end;

//------------------------------------------
//Local IPAddr
function Local_ip_address:longint;
const bufsize=255;
var buf: pointer;
    RemoteHost : PHostEnt;
begin
     buf:=nil;
     try
        getmem(buf,bufsize);
        winsock.gethostname(buf,bufsize);   //this one maybe without domain
        RemoteHost:=Winsock.GetHostByName(buf);
        if RemoteHost=nil then
           Result:=INVALID_IP_ADDRESS
         else
           Result:=longint(pointer(RemoteHost^.h_addr_list^)^);
     finally
        if buf<>nil then freemem(buf,bufsize);
     end;
end;

//------------------------------------------
//Internet_Date produces a fully formatted date string
//e.g. 17/07/01 10:35 -> Tue, 17 Jul 2001 10:35:00 GMT
function internet_date(date: TDateTime):string;

   function LeadingZerosinttostr(value:integer; len:byte):string;
   begin
        result:=inttostr(value);
        while length(result)<len do
              result:='0'+result;
   end;

   function timezone:string;
   var bias: longint;
       tz_info: TTimeZoneInformation;
       {TTimeZoneInformation = record
           Bias: Longint;
           StandardName: array[0..31] of WCHAR;
           StandardDate: TSystemTime;
           StandardBias: Longint;
           DaylightName: array[0..31] of WCHAR;
           DaylightDate: TSystemTime;
           DaylightBias: Longint;
        end;}
   begin
        case GetTimeZoneInformation(tz_info) of
             1: bias:=tz_info.StandardBias;
             2: bias:=tz_info.DaylightBias;
        else
            bias:=0;
        end;
        if bias=0 then
           timezone:='GMT'
          else
           if bias<0 then
              timezone:='+' + LeadingZerosinttostr(abs(bias) div 60,2)
                            + LeadingZerosinttostr(abs(bias) mod 60,2)
             else
              if bias>0 then
                 timezone:='-' + LeadingZerosinttostr(bias div 60,2)
                               + LeadingZerosinttostr(bias mod 60,2);
   end;

var d,m,y,w,h,mm,s,ms: word;
const weekdays:array[1..7] of string[3]=('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
      months:array[1..12] of string[3]=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
begin
     decodedate(date,y,m,d);
     decodetime(date,h,mm,s,ms);
     w:=dayofweek(date);
     internet_date:=weekdays[w]+', '+inttostr(d)+' '+months[m]+' '+inttostr(y)+' '+
         LeadingZerosinttostr(h,2)+':'+LeadingZerosinttostr(mm,2)+':'+LeadingZerosinttostr(s,2)+' '+timezone;
end;

//------------------------------------------
//Convert an API IPAddr to a string
function ip2string(ip_address:longint):string;
begin
     ip_address:=winsock.ntohl(ip_address);
     result:= inttostr(ip_address shr 24)+'.'+
              inttostr((ip_address shr 16) and $ff)+'.'+
              inttostr((ip_address shr 8) and $ff)+'.'+
              inttostr(ip_address and $ff);
end;

//------------------------------------------
//Get the API IPAddr of a host,
//    whether octets or Domain Name
function lookup_hostname(const hostname:string):longint;
var RemoteHost : PHostEnt;
    ip_address: longint;
begin
     ip_address:=INVALID_IP_ADDRESS;
     try
        if hostname='' then
           begin  //no host given!
                result:=ip_address;
                exit;
           end
          else
           begin
                ip_address:=Winsock.Inet_Addr(PChar(hostname));  //try a xxx.xxx.xxx.xx first
                if ip_address=SOCKET_ERROR then
                   begin
                        //try getting by name
                        RemoteHost:=Winsock.GetHostByName(PChar(hostname));
                        if (RemoteHost=nil) or (RemoteHost^.h_length<=0) then
                           begin
                                Result:=ip_address;
                                exit;  //host not found
                           end
                          else
                           ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^);
                           //use the first address given
                   end;
           end;
     except
        ip_address:=INVALID_IP_ADDRESS;
     end;
  result:=ip_address;
end;

//------------------------------------------
//Get the Name assoc. with an API IPAddr
//    if none is specified, return the octet of the IPAddr
function resolve_hostname(ip: longint):string;
var RemoteHost : PHostEnt;
    ip_address: longint;
begin
     ip_address:=ip;
     RemoteHost:=Winsock.GetHostByAddr(@ip_address,4,pf_inet);
     if RemoteHost<>nil then
        result:=pchar(RemoteHost^.h_name)
       else
        result:='Unknown ['+ip2string(ip_address)+']';
end;

//------------------------------------------
//Initialisation and Finalisation Routines
procedure init;
var point: TWSAData;
begin
     tcpip_ready:=false;
     case Winsock.WSAStartup($0101,point) of
          WSAEINVAL, WSASYSNOTREADY, WSAVERNOTSUPPORTED: ;
     else
         tcpip_ready:=true;
     end;
end;

procedure shutdown;
begin
     Winsock.WSACancelBlockingCall;
     Winsock.WSACleanup;
end;

initialization
begin
  init;
end;

finalization
begin
     Shutdown;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -