begin process at 2008 08 22 00:49:10
1 229 733 membres
5 nouveaux aujourd'hui
14 267 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

RÉCUPÉRER SON IP CORRESPONDANT A SON IP INTERNET


Information sur la source

Catégorie :Réseau & Internet Niveau : Débutant Date de création : 19/03/2003 Date de mise à jour : 19/03/2003 00:44:53 Vu : 5 228

Note :
6,38 / 10 - par 8 personnes
6,38 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

Description

Voici comment faire pour obtenir son adresse IP de l'interface connectée sur internet [qui peut être differente de l'IP local si vous avez plusieurs interfaces reseaux]

Source de cette article : http://delphi.about.com/library/weekly/aa103100a.htm

l'article de fabiin pour obtenir son adresse local : http://www.delphifr.com/article.aspx?Val=49.

Source

  • uses Winsock;
  • function GetIPFromHost
  • (var HostName, IPaddr, WSAErr: string): Boolean;
  • type
  • Name = array[0..100] of Char;
  • PName = ^Name;
  • var
  • HEnt: pHostEnt;
  • HName: PName;
  • WSAData: TWSAData;
  • i: Integer;
  • begin
  • Result := False;
  • if WSAStartup($0101, WSAData) <> 0 then begin
  • WSAErr := 'Winsock is not responding."';
  • Exit;
  • end;
  • IPaddr := '';
  • New(HName);
  • if GetHostName(HName^, SizeOf(Name)) = 0 then
  • begin
  • HostName := StrPas(HName^);
  • HEnt := GetHostByName(HName^);
  • for i := 0 to HEnt^.h_length - 1 do
  • IPaddr :=
  • Concat(IPaddr,
  • IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.');
  • SetLength(IPaddr, Length(IPaddr) - 1);
  • Result := True;
  • end
  • else begin
  • case WSAGetLastError of
  • WSANOTINITIALISED:WSAErr:='WSANotInitialised';
  • WSAENETDOWN :WSAErr:='WSAENetDown';
  • WSAEINPROGRESS :WSAErr:='WSAEInProgress';
  • end;
  • end;
  • Dispose(HName);
  • WSACleanup;
  • end;
  • procedure TForm1.Button1Click(Sender: TObject);
  • var
  • Host, IP, Err: string;
  • begin
  • if GetIPFromHost(Host, IP, Err) then begin
  • Edit1.Text := Host;
  • Edit2.Text := IP;
  • end
  • else
  • MessageDlg(Err, mtError, [mbOk], 0);
  • end;
uses Winsock; 

function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean; 
type 
  Name = array[0..100] of Char; 
  PName = ^Name; 
var 
  HEnt: pHostEnt; 
  HName: PName; 
  WSAData: TWSAData; 
  i: Integer; 
begin 
  Result := False;     
  if WSAStartup($0101, WSAData) <> 0 then begin 
    WSAErr := 'Winsock is not responding."'; 
    Exit; 
  end; 
  IPaddr := ''; 
  New(HName); 
  if GetHostName(HName^, SizeOf(Name)) = 0 then
  begin 
    HostName := StrPas(HName^); 
    HEnt := GetHostByName(HName^); 
    for i := 0 to HEnt^.h_length - 1 do 
     IPaddr :=
      Concat(IPaddr,
      IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.'); 
    SetLength(IPaddr, Length(IPaddr) - 1); 
    Result := True; 
  end
  else begin 
   case WSAGetLastError of
    WSANOTINITIALISED:WSAErr:='WSANotInitialised'; 
    WSAENETDOWN      :WSAErr:='WSAENetDown'; 
    WSAEINPROGRESS   :WSAErr:='WSAEInProgress'; 
   end; 
  end; 
  Dispose(HName); 
  WSACleanup; 
end; 



procedure TForm1.Button1Click(Sender: TObject); 
var 
  Host, IP, Err: string; 
begin 
  if GetIPFromHost(Host, IP, Err) then begin 
    Edit1.Text := Host; 
    Edit2.Text := IP; 
  end 
  else 
    MessageDlg(Err, mtError, [mbOk], 0); 
end; 

  • signaler à un administrateur
    Commentaire de iocha le 26/05/2004 14:36:04

    Ca ne marche pas, cette fonction me renvoi l'adresse locale et pas celle internet
    ???

  • signaler à un administrateur
    Commentaire de JulioDelphi le 31/10/2004 17:29:49 administrateur CS

    idem, je suis derriere un routeur

  • signaler à un administrateur
    Commentaire de Nero` le 19/11/2005 13:59:23

    Bidon !!!
    ça revois l'ip local :(

  • signaler à un administrateur
    Commentaire de Bacterius le 16/03/2008 11:04:52

    Ip Local, désolé :(

    Cordialement, Bacterius !

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Boutique

Boutique de goodies CodeS-SourceS