f0xi
Messages postés4205Date d'inscriptionsamedi 16 octobre 2004StatutModérateurDernière intervention12 mars 2022
-
9 juil. 2007 à 11:39
EricStrasbourg
Messages postés4Date d'inscriptionvendredi 7 avril 2006StatutMembreDernière intervention15 août 2007
-
12 août 2007 à 23:41
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.
cincap
Messages postés460Date d'inscriptiondimanche 5 décembre 2004StatutMembreDernière intervention 6 avril 20092 16 juil. 2007 à 18:08
Bonjour Francky23012301,
Bien vu et je savais que tu avais travaillé avec Julen100 sur la partie qui touche aux connections Internet, partie qui n'a plus de secret pour toi.
Ceci dit, il est toujours bon de rappeller qu'il existe des sources ou morceau de source qui rappellent des méthodes méconnues par les débutants dont je fais partie.
@+,
Cincap
Francky23012301
Messages postés400Date d'inscriptionsamedi 6 août 2005StatutMembreDernière intervention11 février 20161 16 juil. 2007 à 17:57
Cincap : Dans la source de JLen il y a des méthodes pour l'IP Local (Voir la partie WInsock) et d'autres pour l'IP internet (Qui avit déjà été pondu par d'autres comme Caribensila). Cependant je te rejoins sur une chose : avec JLen on avait mis en place une technique pour détecter l'adresse IP et l'envoyer par mail à un autre poste.
cincap
Messages postés460Date d'inscriptiondimanche 5 décembre 2004StatutMembreDernière intervention 6 avril 20092 16 juil. 2007 à 08:53
Bonjour à toutes et à tous,
Jlen100 avait trouvé ceci pour le projet "Video surveillance à 4 caméras.."
4 possibilités de trouver son adresse IP.
*****************************************
procedure TFServer.IdHTTPServer1Execute(AThread: TIdPeerThread);
begin
//
end;
{function TFServer.GetLocalIp: string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
ip_machine:string;
begin
{Result:=IdHTTP1.Get('http://filezilla.sourceforge.net/misc/ip.php');
IPServer.Text:=Result;//'Automatic Ip detection: '+
WSAStartup($101, GInitData);
ip_machine:='';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
// l'IP est stockée ci-dessous
ip_machine := StrPas(inet_ntoa(pptr^[I]^));
IPServer.Text:= ip_machine;
Inc(I);
end;
WSACleanup;
end; }
function TFServer.GetLocalIp: string;
var HTMLSource:string;
wsaData: TWSAData;
addr: TSockAddrIn;
Phe: PHostEnt;
szHostName: array[0..128] of Char;
BeginDelimit,EndDelimit:integer;
begin
if RadioButton1.Checked then
begin
try
try
if WSAStartup($101, WSAData) <> 0 then
raise Exception.Create('Cannot initialize WSA');
try
if GetHostName(szHostName, 128) <> SOCKET_ERROR then
begin
Phe := GetHostByName(szHostName);
if Assigned(Phe) then
begin
addr.sin_addr.S_addr := longint(plongint(Phe^.h_addr_list^)^);
Result := inet_ntoa(addr.sin_addr);
end;
end;
finally
WSACleanup;
end;
except
on e:Exception do begin
Result:='Error: '+e.Message;
raise;
end else
raise;
end;
finally
//StaticText1.Caption:='Automatic Ip detection: '+Result;
end;
end;
if RadioButton2.Checked then
begin
Try
HTMLSource:=IdHTTP1.Get('http://checkip.dyndns.org/');
delete(HTMLSource,1,pos(':',HTMLSource)+1);
delete(HTMLSource,pos('<',HTMLSource),80);
result:=HTMLSource;
Except
// Result:=false;
end;
end;
if RadioButton3.Checked then
Try
result:=IdHTTP1.Get('http://filezilla.sourceforge.net/misc/ip.php');
Except
// Result:=false;
end;
if RadioButton4.Checked then
begin
Try
HTMLSource:= IdHTTP1.Get('http://www.showmyip.com/fr/'); //On demande le contenu de la page et on le récupère dans HTMLSource
BeginDelimit:=length(Copy(HTMLSource,1, Pos('Internet',HTMLSource)+ 9 ) );//);76 ,77
EndDelimit:=length(Copy(HTMLSource,Pos('-->',HTMLSource)-1,Length(HTMLSource)));
Result:=Copy(HTMLSource,BeginDelimit,Length(HTMLSource)-BeginDelimit-EndDelimit+1); //On extrait l'adresse IP de HTMLSource
Except
// Result:=false;
end;
end;
IPServer.Text:=Result;//'Automatic Ip detection: '
end;
******************
@+,
Cincap
the_rain16
Messages postés2Date d'inscriptionjeudi 18 mai 2006StatutMembreDernière intervention15 juillet 2007 15 juil. 2007 à 18:47
Bonjour a tous,
merci pour vos commentaires et merci pour votre source "WhiteHippo " :)
En realité j'etais sur un projet ou je devais recevoir l'ip de mon serveur par email pour une durée de 36H (car j'ai une ip dyn), alors cette idee m'est venue en tete en tombant sur le site (www.whatismyip.com), j'ai realisé mon projet puis j'ai collé l'astuce sur le site.
pour vous japee:
On peut toujours developper cette idée en tenant compte a ce que l'appalication ne soit pas dependante d'un parametre X.
Et merci.
WhiteHippo
Messages postés1154Date d'inscriptionsamedi 14 août 2004StatutMembreDernière intervention 5 avril 20123 13 juil. 2007 à 14:02
J'avais fait cette variante, plus succinte :
uses StrUtils, IdHTTP;
function GetPublicIP: AnsiString;
const
ADDRESS = 'Current IP Address: ';
var
i: integer;
s: AnsiString;
AnIdHTTP: TIdHTTP;
begin
Result:='';
AnIdHTTP := TIdHTTP.Create(Form1);
try
s := AnIdHTTP.Get( 'http://checkip.dyndns.org' );
// Résultat de la forme :
// <html><head><title>Current IP Check</title></head> Current IP Address: 255.255.255.255 </html>
// Recherche position début adresse IP
i : = AnsiPos(ADDRESS,s)+Length(ADDRESS);
// Recherche du < de
while s[i]<>'<' do
begin
Result := Result + s[i];
inc(i);
end;
finally
FreeAndNil( AnIdHTTP );
end;
end;
// Exemple d'appel
procedure TForm1.Button1Click(Sender: TObject);
begin
caption := GetPublicIP;
end;
Cordialement.
japee
Messages postés1727Date d'inscriptionvendredi 27 décembre 2002StatutModérateurDernière intervention 6 novembre 20218 11 juil. 2007 à 15:06
"Il faudra donc faire une 2ème requête"
... ou récupérer le contenu du presse-papier, bien entendu !
japee
Messages postés1727Date d'inscriptionvendredi 27 décembre 2002StatutModérateurDernière intervention 6 novembre 20218 11 juil. 2007 à 15:03
Salut,
Intéressante approche du problème.
Difficile néanmoins de se passer de www.whatismyip.com ou autre :p
Petit bug : s'il reste du texte dans le presse-papiers, c'est lui qui sera retourné par la fonction et non pas l'adresse IP. Il faudra donc faire une 2ème requête... :(
the_rain16
Messages postés2Date d'inscriptionjeudi 18 mai 2006StatutMembreDernière intervention15 juillet 2007 9 juil. 2007 à 15:52
Salut f0xi et merci pour ton commentaire.
oui c'est exacte j'utilise dans ma dll un type string, moi je croyai que c'etait un bug de traitement!!! :(
mais bon, je vai revoir mon code.
pour la source de la dll, je ne cache pas, c'est juste un petit truc de magie :)
il y'a un site web qui te retourne ton ip wan (www.whatismyip.com) avec un petit script java qui charge l'ip dans le presse papier.
alors j'utilise le composant indy IDhttp pour charger la page du site et je simule le click pour activer le script java, et en retour j'ai l'ip en presse papier.
Voila l'astuce.
ces lignes de code sont ici pour eviter que l'anti virus (dans mans cas kaspersky)
declanche une alerte type autorisation
}
end;
while (Cardinal(IEApp.readyState)<> 4) and (i<6) do
begin
sleep(1000);
i:=i+1;
end;
if i<=6 then
begin
IEApp.Document.links.item(0).click;
getip:=clipboard.AsText
end;
end;
exports
getip;
begin
end.
vos commentaires sont les bienvenue.
et merci encore une fois f0xi :)
f0xi
Messages postés4205Date d'inscriptionsamedi 16 octobre 2004StatutModérateurDernière intervention12 mars 202235 9 juil. 2007 à 11:39
salut
alors, pourquoi ne pas mettre le code de la dll ? la ce n'est pas interessant du tout !
ensuite, on est en delphi pas en c++, alors les identifiant x_machin_y on evite :
GetMyWanIp par exemple decrirais bien le nom de ta fonction.
ensuite, le probleme de pointeur viens surtout du fait que tu utilise le type string!
il faut utiliser le type PChar ou ShortString de cette façon :
const
MaxIpLength = 15;
function GetMyWanIp(var MyIp : PChar) : integer;
a utiliser comme suit :
var MyIp : string;
TtSz : integer;
begin
SetLength(MyIp, MaxIpLength);
TtSz := GetMyWanIp(PChar(MyIp));
SetLength(MyIp, TtSz);
caption := 'votre IP Wan est : '+MyIp;
end;
sinon une autre methode qui consiste a stocker l'ip dans un entier 32bit (cardinal ou tableau de byte) :
type
pIPv4 = ^TIPv4;
TIPv4 = record
case integer of
0 : (Long : LongWord);
1 : (Bytes: array[0..3] of byte);
end;
procedure GetMyWanIP(var IP : LongWord);
a utiliser comme suit :
var
MyIP : TIPv4;
begin
GetMyWanIP(MyIP.Long);
caption := 'Votre IP Wan est : '+format('%d.%d.%d.%d',[MyIP.Bytes[0],MyIP.Bytes[1],MyIP.Bytes[2],MyIP.Bytes[3],]);
end;
qui sera d'ailleur plus facile que l'utilisation de PChar...
voila, bonne prog et oublis pas la source de la DLL.
12 août 2007 à 23:41
Pourquoi ne pas employer très simplement le composant Indy TIdHTTP ?
Auquel cas ces quelques ligne pour récupérer l'IP publique suffisent :
//__________ Récupérer l'IP publique
url := 'http://dynupdate.no-ip.com/ip.php';
HTTP := TIdHTTP.Create(nil);
Try
Try
lIP := Http.Get(url);
Except
lIP := '';
End ;
Finally
FreeAndNil(http);
End;
Cdlt,
Eric
16 juil. 2007 à 18:08
Bien vu et je savais que tu avais travaillé avec Julen100 sur la partie qui touche aux connections Internet, partie qui n'a plus de secret pour toi.
Ceci dit, il est toujours bon de rappeller qu'il existe des sources ou morceau de source qui rappellent des méthodes méconnues par les débutants dont je fais partie.
@+,
Cincap
16 juil. 2007 à 17:57
16 juil. 2007 à 08:53
Jlen100 avait trouvé ceci pour le projet "Video surveillance à 4 caméras.."
4 possibilités de trouver son adresse IP.
*****************************************
procedure TFServer.IdHTTPServer1Execute(AThread: TIdPeerThread);
begin
//
end;
{function TFServer.GetLocalIp: string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
ip_machine:string;
begin
{Result:=IdHTTP1.Get('http://filezilla.sourceforge.net/misc/ip.php');
IPServer.Text:=Result;//'Automatic Ip detection: '+
WSAStartup($101, GInitData);
ip_machine:='';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
// l'IP est stockée ci-dessous
ip_machine := StrPas(inet_ntoa(pptr^[I]^));
IPServer.Text:= ip_machine;
Inc(I);
end;
WSACleanup;
end; }
function TFServer.GetLocalIp: string;
var HTMLSource:string;
wsaData: TWSAData;
addr: TSockAddrIn;
Phe: PHostEnt;
szHostName: array[0..128] of Char;
BeginDelimit,EndDelimit:integer;
begin
if RadioButton1.Checked then
begin
try
try
if WSAStartup($101, WSAData) <> 0 then
raise Exception.Create('Cannot initialize WSA');
try
if GetHostName(szHostName, 128) <> SOCKET_ERROR then
begin
Phe := GetHostByName(szHostName);
if Assigned(Phe) then
begin
addr.sin_addr.S_addr := longint(plongint(Phe^.h_addr_list^)^);
Result := inet_ntoa(addr.sin_addr);
end;
end;
finally
WSACleanup;
end;
except
on e:Exception do begin
Result:='Error: '+e.Message;
raise;
end else
raise;
end;
finally
//StaticText1.Caption:='Automatic Ip detection: '+Result;
end;
end;
if RadioButton2.Checked then
begin
Try
HTMLSource:=IdHTTP1.Get('http://checkip.dyndns.org/');
delete(HTMLSource,1,pos(':',HTMLSource)+1);
delete(HTMLSource,pos('<',HTMLSource),80);
result:=HTMLSource;
Except
// Result:=false;
end;
end;
if RadioButton3.Checked then
Try
result:=IdHTTP1.Get('http://filezilla.sourceforge.net/misc/ip.php');
Except
// Result:=false;
end;
if RadioButton4.Checked then
begin
Try
HTMLSource:= IdHTTP1.Get('http://www.showmyip.com/fr/'); //On demande le contenu de la page et on le récupère dans HTMLSource
BeginDelimit:=length(Copy(HTMLSource,1, Pos('Internet',HTMLSource)+ 9 ) );//);76 ,77
EndDelimit:=length(Copy(HTMLSource,Pos('-->',HTMLSource)-1,Length(HTMLSource)));
Result:=Copy(HTMLSource,BeginDelimit,Length(HTMLSource)-BeginDelimit-EndDelimit+1); //On extrait l'adresse IP de HTMLSource
Except
// Result:=false;
end;
end;
IPServer.Text:=Result;//'Automatic Ip detection: '
end;
******************
@+,
Cincap
15 juil. 2007 à 18:47
merci pour vos commentaires et merci pour votre source "WhiteHippo " :)
En realité j'etais sur un projet ou je devais recevoir l'ip de mon serveur par email pour une durée de 36H (car j'ai une ip dyn), alors cette idee m'est venue en tete en tombant sur le site (www.whatismyip.com), j'ai realisé mon projet puis j'ai collé l'astuce sur le site.
pour vous japee:
On peut toujours developper cette idée en tenant compte a ce que l'appalication ne soit pas dependante d'un parametre X.
Et merci.
13 juil. 2007 à 14:02
uses StrUtils, IdHTTP;
function GetPublicIP: AnsiString;
const
ADDRESS = 'Current IP Address: ';
var
i: integer;
s: AnsiString;
AnIdHTTP: TIdHTTP;
begin
Result:='';
AnIdHTTP := TIdHTTP.Create(Form1);
try
s := AnIdHTTP.Get( 'http://checkip.dyndns.org' );
// Résultat de la forme :
// <html><head><title>Current IP Check</title></head> Current IP Address: 255.255.255.255 </html>
// Recherche position début adresse IP
i : = AnsiPos(ADDRESS,s)+Length(ADDRESS);
// Recherche du < de
while s[i]<>'<' do
begin
Result := Result + s[i];
inc(i);
end;
finally
FreeAndNil( AnIdHTTP );
end;
end;
// Exemple d'appel
procedure TForm1.Button1Click(Sender: TObject);
begin
caption := GetPublicIP;
end;
Cordialement.
11 juil. 2007 à 15:06
... ou récupérer le contenu du presse-papier, bien entendu !
11 juil. 2007 à 15:03
Intéressante approche du problème.
Difficile néanmoins de se passer de www.whatismyip.com ou autre :p
Petit bug : s'il reste du texte dans le presse-papiers, c'est lui qui sera retourné par la fonction et non pas l'adresse IP. Il faudra donc faire une 2ème requête... :(
9 juil. 2007 à 15:52
oui c'est exacte j'utilise dans ma dll un type string, moi je croyai que c'etait un bug de traitement!!! :(
mais bon, je vai revoir mon code.
pour la source de la dll, je ne cache pas, c'est juste un petit truc de magie :)
il y'a un site web qui te retourne ton ip wan (www.whatismyip.com) avec un petit script java qui charge l'ip dans le presse papier.
alors j'utilise le composant indy IDhttp pour charger la page du site et je simule le click pour activer le script java, et en retour j'ai l'ip en presse papier.
Voila l'astuce.
et voici le code source
library get_public_ip;
uses
Variants,
windows,
comobj,
IdHTTP,
Clipbrd;
{$R *.res}
var
IEApp: OLEVariant;
i:integer;
function getip:string; stdcall;
begin
i:=0;
if(VarIsEmpty(IEApp))then
begin
IEApp := CreateOleObject('InternetExplorer.Application');
ieapp.top:=8000;
ieapp.visible:=true;
IEApp.Navigate('http://www.whatismyip.com/');
ieapp.visible:=false;
{
ieapp.top:=8000;
ieapp.visible:=true;
ieapp.visible:=false;
ces lignes de code sont ici pour eviter que l'anti virus (dans mans cas kaspersky)
declanche une alerte type autorisation
}
end;
while (Cardinal(IEApp.readyState)<> 4) and (i<6) do
begin
sleep(1000);
i:=i+1;
end;
if i<=6 then
begin
IEApp.Document.links.item(0).click;
getip:=clipboard.AsText
end;
end;
exports
getip;
begin
end.
vos commentaires sont les bienvenue.
et merci encore une fois f0xi :)
9 juil. 2007 à 11:39
alors, pourquoi ne pas mettre le code de la dll ? la ce n'est pas interessant du tout !
ensuite, on est en delphi pas en c++, alors les identifiant x_machin_y on evite :
GetMyWanIp par exemple decrirais bien le nom de ta fonction.
ensuite, le probleme de pointeur viens surtout du fait que tu utilise le type string!
il faut utiliser le type PChar ou ShortString de cette façon :
const
MaxIpLength = 15;
function GetMyWanIp(var MyIp : PChar) : integer;
a utiliser comme suit :
var MyIp : string;
TtSz : integer;
begin
SetLength(MyIp, MaxIpLength);
TtSz := GetMyWanIp(PChar(MyIp));
SetLength(MyIp, TtSz);
caption := 'votre IP Wan est : '+MyIp;
end;
sinon une autre methode qui consiste a stocker l'ip dans un entier 32bit (cardinal ou tableau de byte) :
type
pIPv4 = ^TIPv4;
TIPv4 = record
case integer of
0 : (Long : LongWord);
1 : (Bytes: array[0..3] of byte);
end;
procedure GetMyWanIP(var IP : LongWord);
a utiliser comme suit :
var
MyIP : TIPv4;
begin
GetMyWanIP(MyIP.Long);
caption := 'Votre IP Wan est : '+format('%d.%d.%d.%d',[MyIP.Bytes[0],MyIP.Bytes[1],MyIP.Bytes[2],MyIP.Bytes[3],]);
end;
qui sera d'ailleur plus facile que l'utilisation de PChar...
voila, bonne prog et oublis pas la source de la DLL.