首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

DELPHI怎么获得客户端IP!

2012-02-20 
DELPHI如何获得客户端IP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!在DELPHI里如何获得客户端的IP呢?有像JAVA里面reque

DELPHI如何获得客户端IP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
在DELPHI里如何获得客户端的IP呢?有像JAVA里面request.getRemoteAddr()函数吗

[解决办法]
function G_GetLocalHostName(): string;
var
wVersionRequested: WORD;
wsaData: TWSAData;
p: PHostEnt;
s: array[0..128] of char;
begin
result := '';
try
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
GetHostName(@s, 128);
p := GetHostByName(@s);
result := p^.h_Name;
WSACleanup;
except
end;
end;

function G_GetLocalHostIp(): string;
var
wVersionRequested: WORD;
wsaData: TWSAData;
p: PHostEnt;
s: array[0..128] of char;
begin
result := '';
try
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
GetHostName(@s, 128);
p := GetHostByName(@s);
result := inet_ntoa(PInAddr(p^.h_addr_list^)^);
WSACleanup();
except
end;
end;

客户端登录时,账号密码验证通过后,把自己的IP提交给服务端做处理就可以了。
[解决办法]
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of char;
I: Integer;
GInitData: TWSADATA;
Result:string;
AName: pchar;
asize:cardinal;

begin

asize:=MAX_COMPUTERNAME_LENGTH + 1;
getmem(AName,asize);
try
getcomputername(AName,asize);
finally
freemem(AName);
end;

AName :='jujumao';
WSAStartup($101, GInitData);

StrPCopy(Buffer, AName);

phe := GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do
begin
Result := StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
edit1.Text :=Result;

end;
WSACleanup;
end;

end.

热点排行