Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#4

Re: Über IP den Hostname ermitteln

  Alt 5. Okt 2005, 11:31
Man kann auch direkt mit Winsock arbeiten.
Delphi-Quellcode:
unit WinSockTools;

interface

function ResolveIP(const ipadrstr:string):string;


procedure RaiseSocketError(const UserMsg:string);


implementation

uses WinSock, SysUtils;

function ResolveIP(const ipadrstr:string):string;
var
  InetAddr: u_long;
  HostEntPtr: PHostEnt;
  HostEnt: THostEnt;
  len, struct: Integer;
  i: Integer;
begin
  if ipadrstr = '255.255.255.255then
  begin
      Result := 'broadcast';
      Exit;
  end;


    // Convert textual IP address to binary format
    InetAddr := inet_addr( PChar(ipadrstr) );
    if InetAddr = SOCKET_ERROR then
      raise Exception.Create( 'Invalid address entered' );
    len := 4;

    // Get hist entry by IP
    HostEntPtr := GetHostByAddr( @InetAddr, len, AF_INET );
    if HostEntPtr = NIL then
       RaiseSocketError('GetHostByAddr');

    // Insert hostname into list
    Result := String( HostEntPtr^.h_name );
end;


procedure RaiseSocketError(const UserMsg:string);
begin
   raise Exception.Create('WinSSock error:'#13#10+UserMsg+#13#10+
      SysErrorMessage(WSAGetLastError()));
end;


end.
Andreas
  Mit Zitat antworten Zitat