//取本地 IP (多IP)
function GetLocalIPs(var LocalIp: array of string; var ipnum: Integer): Boolean;
var
HostEnt: PHostEnt;
Ip: array [0…10] of string;
addr: pchar;
Buffer: array [0…63] of char;
GInitData: TWSADATA;
i: Integer;
begin
Result := False;
try
WSAStartup(2, GInitData);
GetHostName(Buffer, SizeOf(Buffer));
HostEnt := gethostbyname(buffer);
addr := HostEnt^.h_addr_list^;
if HostEnt = nil then Exit;
ipnum := (Length(addr) div 2) div HostEnt^.h_length;
for i := 0 to IPNum do
begin
ip[i] := Format(‘%d.%d.%d.%d’, [byte(addr [i*4+0]),
byte (addr [i*4+1]), byte (addr [i*4+2]), byte (addr [i*4+3])]);
LocalIp[i] := Ip[i];
end;
Result := True;
finally
WSACleanup;
end;
end;
///////////////////////////////////////////////////////////////
函数的调用方法:
var
LIPs: array [0…10] of string;
IPn: Integer;
// 服务器IP
GetLocalIPs(LIPs,IPn);