this slowpoke moves

Get IP from Host

Beispiel 1 :
uses Winsock

//

function GetIPFromHostname (HostName : String): String;
type
  Name =array[0..254] of Char;
  PName = ^Name;
var
  HEnt: pHostEnt;
  HName: PName;
  WSAData: TWSAData;
  i: Integer;
begin
  Result := '';
  if WSAStartup($0101, WSAData) <> 0 then Exit;
  New(HName);
  StrPCopy(PChar(HName),HostName);
  HEnt := GetHostByName(HName^);
  for i := 0 to HEnt^.h_length - 1 do
  Result :=
  Concat(Result,IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.');
  SetLength(Result, Length(Result) - 1);
  Dispose(HName);
  WSACleanup;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := GetIPFromHostname ('Host')
end;
Beispiel 2 :
function GetIPAddress(const HostName: string): string;
var
  R: Integer;
  WSAData: TWSAData;
  HostEnt: PHostEnt;
  Host: string;
  SockAddr: TSockAddrIn;
begin
  Result := '';
  R := WSAStartup($0101, WSAData);
  if R = 0 then
  try
    Host := HostName;
    if Host = '' then
    begin
      SetLength(Host, MAX_PATH);
      GetHostName(@Host[1], MAX_PATH);
    end;
    HostEnt := GetHostByName(@Host[1]);
    if HostEnt <> nil then
    begin
      SockAddr.sin_addr.S_addr := Longint(PLongint(HostEnt^.h_addr_list^)^);
      Result := inet_ntoa(SockAddr.sin_addr);
    end;
  finally
    WSACleanup;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1.Caption := GetIPAddress('google.com');
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate