|
Постоянный
Регистрация: 26.12.2007
Сообщений: 360
С нами:
9671366
Репутация:
332
|
|
Сообщение от Maxxxtri23
Как узнать IP за NAT'ом? Вот у меня есть код, но он под 5 делфи, а у меня 7, кто может переписать?
Код:
{
This code requires a server running a script that generates a web
page that has your IP address in it.
This example uses http://www.whatismyip.com/
The script then searches for the first time
that 'Your ip is ' shows up and then looks for a
correctly formatted IP address from that point.
This works using Delphi 6 Enterprise but should work with previous
versions with little or no modification, I think.
}
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,
Graphics, Controls, Forms, Dialogs, Psock,
NMHttp, StdCtrls, ExtCtrls, Menus, About,
Winsock, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
NMHTTP1: TNMHTTP;
Memo1: TMemo;
Edit1: TEdit;
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
Bevel1: TBevel;
Bevel2: TBevel;
procedure Button1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ClipBrd;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
InetIP: string;
WebAddress, SearchString: string;
Buff, P, FT: PChar;
BuffLen: Word;
StartPos, StringLength, TempInt: Integer;
begin
WebAddress := 'http://www.whatismyip.com/';
SearchString := 'Your ip is ';
Memo1.Clear;
try
NMHTTP1.Get(WebAddress);
except
on E: Exception do
begin
MessageDlg('Could not get IP Address! ' +
'Please ensure you are connected to ' +
'the Internet.', mtError, [mbOK], 0);
end;
end;
Memo1.Text := NMHTTP1.Body;
Memo1.SelStart := 0;
GetMem(FT, Length(SearchString) + 1);
StrPCopy(FT, SearchString);
BuffLen := Memo1.GetTextLen + 1;
GetMem(Buff, BuffLen);
Memo1.GetTextBuf(Buff, BuffLen);
P := Buff + Memo1.SelStart + Memo1.SelLength;
P := StrPos(P, FT);
if P = nil then MessageBeep(0)
else
begin
Memo1.SelStart := P - Buff;
Memo1.SelLength := Length(SearchString);
end;
StringLength := Memo1.SelLength;
StartPos := Memo1.SelStart + StringLength;
tempint := StartPos;
InetIP := '';
while ((Buff[TempInt] in ['0'..'9']) or
(Buff[TempInt] = '.')) do
begin
InetIP := InetIP + Buff[TempInt];
tempint := tempint + 1;
end;
FreeMem(FT, Length(SearchString) + 1);
FreeMem(Buff, BuffLen);
Edit1.Text := InetIP;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
end.
я так понял ты с кодом не разбирался..... и с веб в дэлфи неработал... да небеда.
этот код обращается к сайту http://www.whatismyip.com/ на который ты можеш зайти и увидеть что на сайте будет показан твой ip в глобальной сети
Your IP Address Is
0.0.0.0
прога обращается к сайту загружает страницу на которой написан твой ip и просто парсит ip. таких сайтов много.
ну да ладно болтовни лови вот накалякал немного:
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, ExtCtrls;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
res:string;
public
{ Public declarations }
end;
var
Form1: TForm1;
i:integer;
const sended: AnsiString = 'GET /automation/n09230945.asp HTTP/1.1' + #$D#$A +
'User-Agent: Mozilla/4.0 (compatible; MSIE 99.01; Windows 7)' + #$D#$A +
'Host: www.whatismyip.com' + #$D#$A +
'Connection: Keep-Alive' + #$D#$A + #$D#$A;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
res:='';
i:=0;
clientsocket1.Host:='www.whatismyip.com';
clientsocket1.port:=80;
clientsocket1.open;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
res:=socket.ReceiveText;
i:=strtoint(copy(res,pos('th: ',res)+4,2));
edit1.Text:=copy(res,length(res)-i+1,i);
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
socket.SendText(sended);
end;
end.
все теперь в едите1 твой отпарсенный ip
P.S. кстати у этого кода есть приемущество... он парсит страницу которая весит от силы 300байт
Последний раз редактировалось KIR@PRO; 23.02.2009 в 00:20..
|