1.本机IP 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; 2.对方IP ServerSocket1.Socket.Connections[i].RemoteAddress
把客户端IP一块发到服务器,然后进行分解!获得本机IP: procedure TF_main.getipaddress; //»ñµÃ±¾»úIPµØÖ· var ip,ipstr:string; ch:array[1..32] of char; i:integer; wsdata:twsadata; myhost:phostent; begin if wsastartup(2,wsdata)<>0 then begin messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0); halt(2); end; try if gethostname(@ch[1],32)<>0 then begin messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0); halt(3); end; except messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0); halt(3); end; myhost:=gethostbyname(@ch[1]); if myhost=nil then begin messagedlg('ipµØַΪ¿Õ',mtinformation,[mbok],0); halt(4); end else begin for i:=1 to 4 do begin ip:=inttostr(ord(myhost.h_addr^[i-1])); ipstr:=ipstr+ip; if i<4 then ipstr:=ipstr+'.' else strIP:=ipstr; end; end; end;你可以写成函数
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;
2.对方IP
ServerSocket1.Socket.Connections[i].RemoteAddress
x:=Powersock1.LocalIP;
对方的IP:如果你使用的是WinSocket在这个事件中得到
procedure TSocketData.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
ClientIP:=Socket.RemoteAddress;
end;
procedure TF_main.getipaddress; //»ñµÃ±¾»úIPµØÖ·
var ip,ipstr:string;
ch:array[1..32] of char;
i:integer;
wsdata:twsadata;
myhost:phostent;
begin
if wsastartup(2,wsdata)<>0
then
begin
messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0);
halt(2);
end;
try
if gethostname(@ch[1],32)<>0 then
begin
messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0);
halt(3);
end;
except
messagedlg('ûÓгɹ¦·µ»Ø£¡',mtinformation,[mbok],0);
halt(3);
end;
myhost:=gethostbyname(@ch[1]);
if myhost=nil then
begin
messagedlg('ipµØַΪ¿Õ',mtinformation,[mbok],0);
halt(4);
end
else
begin
for i:=1 to 4 do
begin
ip:=inttostr(ord(myhost.h_addr^[i-1]));
ipstr:=ipstr+ip;
if i<4 then
ipstr:=ipstr+'.'
else
strIP:=ipstr;
end;
end;
end;你可以写成函数
是需要uses winsock
function tloginfrm.getip:string;
//返回IP
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
// returns ISP assigned IPf 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;
uses
...
WinSock,
...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;
在Delphi6可以通过
dclsockets70.bpl(Borland Socket Components)
包括的控件:TClientSocket、TServerSocket