function LocalIP(): 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; begin WSAStartup($101,GInitData); Result:=''; 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 result:=Strpas(inet_ntoa(pptr^[i]^)); inc(i); end; WSACleanup; end;
一个Dll,//////library IpName;uses SysUtils, Classes, windows, winsock;{$R *.res}/////得到计算机名字函数//// function GetName: OleVariant;stdcall; const MAX = MAX_COMPUTERNAME_LENGTH; var Computer: array[0..MAX]of Char; Size: DWORD; Str : String; begin Size := MAX; FillChar(Computer, MAX, 0); GetComputerName(Computer, Size); Str := Computer; Result := Str; end;/////得到计算机IP地址函数//// function GetIp: PChar;stdcall; var wVersion: Word; sData: TWSAData; P: PHostEnt; S: array[0..128]of char; begin wVersion := MakeWord(1,1); WSAStartup(wVersion,sData); try gethostname(@S,128); P := gethostbyname(@S); Result := inet_ntoa(PinAddr(P^.h_addr_list^)^); finally WSACleanup; end; end;exports GetName, GetIp;begin end.
//============================================================================== //取得客户端‘主机名’‘IP地址’************************************************ //============================================================================== procedure GetClientInfo(var ClientName, IPAddress: string); var WSAData: TWSAData; HostEnt: PHostEnt; begin {no error checking...} WSAStartup(2, WSAData); SetLength(ClientName, 255); Gethostname(PChar(ClientName), 255); SetLength(ClientName, StrLen(PChar(ClientName))); HostEnt := GetHostByName(PChar(ClientName)); with HostEnt^ do IPAddress := Format('%d.%d.%d.%d',[Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]); WSACleanup; end;
program get_ip; uses winsock,sysutils; VAR ch :ARRAY[1..32] OF Char; i : Integer; WSData:TWSAData; MyHost:PHostEnt; begin IF WSAstartup(2,wsdata)<>0 THEN BEGIN Writeln('can''t start Winsock: Error ',WSAGetLastError); Halt(2); END; try IF getHostName(@ch[1],32)<>0 THEN BEGIN Writeln('getHostName failed'); Halt(3); END; except Writeln('getHostName failed'); halt(3); end; MyHost:=GetHostByName(@ch[1]); IF MyHost=NIL THEN BEGIN Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error '+IntToStr(WSAGetLastError)); Halt(4); END ELSE BEGIN Write('address '); FOR i:=1 TO 4 DO BEGIN Write(Ord(MyHost.h_addr^[i-1])); IF i<4 THEN write('.') ELSE writeln; END; END; end.
type
TaPInAddr = array [0..10] of PInAddr;
PaPinAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer: array [0..63] of char;
i : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101,GInitData);
Result:='';
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
result:=Strpas(inet_ntoa(pptr^[i]^));
inc(i);
end;
WSACleanup;
end;
SysUtils,
Classes,
windows,
winsock;{$R *.res}/////得到计算机名字函数////
function GetName: OleVariant;stdcall;
const
MAX = MAX_COMPUTERNAME_LENGTH;
var
Computer: array[0..MAX]of Char;
Size: DWORD;
Str : String;
begin
Size := MAX;
FillChar(Computer, MAX, 0);
GetComputerName(Computer, Size);
Str := Computer;
Result := Str;
end;/////得到计算机IP地址函数////
function GetIp: PChar;stdcall;
var
wVersion: Word;
sData: TWSAData;
P: PHostEnt;
S: array[0..128]of char;
begin
wVersion := MakeWord(1,1);
WSAStartup(wVersion,sData);
try
gethostname(@S,128);
P := gethostbyname(@S);
Result := inet_ntoa(PinAddr(P^.h_addr_list^)^);
finally
WSACleanup;
end;
end;exports
GetName, GetIp;begin
end.
//取得客户端‘主机名’‘IP地址’************************************************
//==============================================================================
procedure GetClientInfo(var ClientName, IPAddress: string);
var WSAData: TWSAData;
HostEnt: PHostEnt;
begin
{no error checking...}
WSAStartup(2, WSAData);
SetLength(ClientName, 255);
Gethostname(PChar(ClientName), 255);
SetLength(ClientName, StrLen(PChar(ClientName)));
HostEnt := GetHostByName(PChar(ClientName));
with HostEnt^ do IPAddress := Format('%d.%d.%d.%d',[Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
WSACleanup;
end;
uses winsock,sysutils;
VAR
ch :ARRAY[1..32] OF Char;
i : Integer;
WSData:TWSAData;
MyHost:PHostEnt;
begin
IF WSAstartup(2,wsdata)<>0 THEN BEGIN
Writeln('can''t start Winsock: Error ',WSAGetLastError);
Halt(2);
END;
try
IF getHostName(@ch[1],32)<>0 THEN BEGIN
Writeln('getHostName failed');
Halt(3);
END;
except
Writeln('getHostName failed');
halt(3);
end;
MyHost:=GetHostByName(@ch[1]);
IF MyHost=NIL THEN BEGIN
Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error '+IntToStr(WSAGetLastError));
Halt(4);
END ELSE BEGIN
Write('address ');
FOR i:=1 TO 4 DO BEGIN
Write(Ord(MyHost.h_addr^[i-1]));
IF i<4 THEN write('.')
ELSE writeln;
END;
END;
end.