Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi IP über Delphi bestimmen! (https://www.delphipraxis.net/73593-ip-ueber-delphi-bestimmen.html)

wursthunter 20. Jul 2006 10:39


IP über Delphi bestimmen!
 
Ich hab n simples Chatprogramm. Ich will lediglich die IP Adresse von Delphi bestimmen lassn (meine eigene) und sie nich immer selbst aufschreiben. Wie geht das?

Klaus01 20. Jul 2006 10:48

Re: IP über Delphi bestimmen!
 
Kannst es ja mal damit versuchen.

Grüße
Klaus

Delphi-Quellcode:
function HostNameToIP(HostName: string): u_long;
  var
    RemoteHost: PHostEnt;
  begin
    result := u_long(INADDR_NONE);
    if HostName <> '' then
    try
      result := inet_addr(PChar(HostName));                   // try a xxx.xxx.xxx.xxx first
      if result = u_long(INADDR_NONE) then
      begin
        RemoteHost := GetHostByName(PChar(HostName));
        if not ((RemoteHost = NIL) or (RemoteHost^.h_length <= 0)) then
          result := u_long(pointer(RemoteHost^.h_addr_list^)^);
      end;
    except
      result := u_long(INADDR_NONE);
    end;
  end;


  function OwnHostName: string;
  var
    buf      : pointer;
    RemoteHost: PHostEnt;
  begin
    result := '';
    buf := NIL;
    try
      getmem(buf, 255);
      GetHostName(buf, 255);
      if char(buf^) <> #0 then
      begin
        RemoteHost := GetHostByName(buf);
        if RemoteHost <> NIL then result := RemoteHost^.h_name
                             else result := '127.0.0.1';
      end else result := '127.0.0.1';
    finally
      if buf <> NIL then FreeMem(buf, 255);
    end;
  end;


  function OwnIPAddress: u_long;
  begin
    result := HostNameToIP(OwnHostName);
  end;
oder hiermit:

Delphi-Quellcode:
function GetLocalIPs: String;
type PPInAddr= ^PInAddr;
var
  wsaData : TWSAData;
  HostInfo : PHostEnt;
  HostName : Array[0..255] of Char;
  Addr    : PPInAddr;
begin
  Result:='';
  if WSAStartup($0102, wsaData) <> 0 then
    Exit;
  try
    if GetHostName(HostName, SizeOf(HostName)) <> 0 then
      Exit;
    HostInfo:= GetHostByName(HostName);
    if HostInfo=nil then
      Exit;
    Addr:=Pointer(HostInfo^.h_addr_list);
    if (Addr=nil) or (Addr^=nil) then
      Exit;
    Result:=StrPas(inet_ntoa(Addr^^));
    inc(Addr);
    while Addr^ <> nil do begin
      Result:=Result+^M^J+StrPas(inet_ntoa(Addr^^));
      inc(Addr);
    end;
  finally
    WSACleanup;
  end;
end;

fkerber 20. Jul 2006 10:50

Re: IP über Delphi bestimmen!
 
Hi!

Meinst du deine lokale IP-Adresse oder die Internet-IP?
Für beide Fälle sollten sich aber eigentlich genügend Beispiele im Forum finden....


Ciao Frederic

richie 20. Jul 2006 10:57

Re: IP über Delphi bestimmen!
 
Kannst ja die Indy IPWatch-Komponente verwenden! :idea:

inherited 20. Jul 2006 11:18

Re: IP über Delphi bestimmen!
 
Zitat:

Zitat von richie
Kannst ja die Indy IPWatch-Komponente verwenden...

...welhe nicht für Internet-IPs funktioniert.
Meinst du die IP des Servers herauszubekommen?
-> www.dyndns.org


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz