你可以列举当前所TCP连接或UDP连接。然后根据服务器地址()的IP查找出当前连接的IP。
方法1:
Function GetTcpTable(Foreign: String): String;
Const
csNetstat = 'netstat -n -p tcp >%s'+#13+#10+'Exit';
var
ts: TStringlist;
i, j, z: integer;
TcpTableFile, BatFile, WinDir: String;
function GetWinDir: string;
var
SystemDir: array[0..255] of Char;
begin
GetWindowsDirectory( @SystemDir,255);
Result := StrPas(SystemDir);
end;
begin
try
ts := TStringlist.Create;
WinDir := GetWinDir; //
BatFile := WinDir+'/Net.bat';
TcpTableFile := WinDir+'/netstat.log';
ts.Add(Format(csNetstat,[TcpTableFile]));
ts.SaveToFile(BatFile);
winexec(Pchar(BatFile), SW_HIDE);
Sleep(20);
ts.LoadFromFile(TcpTableFile);
for i := 0 to ts.Count-1 do
begin
j := Pos(Foreign, ts.Strings);
z := Pos('ESTABLISHED', ts.Strings);
if (j>0) and (z>0) then
Result := ts.Strings;
end;
ts.Free;
except
Result := '';
end;
end;
方法2:
unit untIPHLPAPI;
interface
uses
Windows, sysutils, WinSock;
type
EIpHlpError = class(Exception);
//----------------TCP结构------------------------------------------------
PTMibTCPRow = ^TMibTCPRow;
TMibTCPRow = packed record
dwState : DWORD;
dwLocalAddr : DWORD;
dwLocalPort : DWORD;
dwRemoteAddr: DWORD;
dwRemotePort: DWORD;
end;
//
PTMibTCPTable = ^TMibTCPTable;
TMibTCPTable = packed record
dwNumEntries : DWORD;
Table : array[0..0] of TMibTCPRow;
end;
//------------------从IPHLPAPI.DLL输入的API函数-----------------------------------
function GetTcpTable(pTcpTable: PTMibTCPTable; var pdwSize: DWORD;
bOrder: BOOL): DWORD; stdcall;
procedure VVGetTcpTable(var pTcpTable: PTMibTCPTable; var dwSize: DWORD;
const bOrder: BOOL);
function GetRemoteTcpAddr(AddrPort: String): String;
implementation
resourcestring
sNotImplemented = 'Function %s is not implemented.';
sInvalidParameter = 'Function %s. Invalid parameter';
sNoData = 'Function %s. No adapter information exists for the local computer.';
sNotSupported = 'Function %s is not supported by the operating system.';
const
iphlpapilib = 'iphlpapi.dll';
function GetTcpTable; external iphlpapilib name 'GetTcpTable';
function IpAddressToString(Addr: DWORD): string;
var
InAddr: TInAddr;
begin
InAddr.S_addr := Addr;
Result := inet_ntoa(InAddr);
end;
procedure IpHlpError(const FunctionName: string; ErrorCode: DWORD);
begin
case ErrorCode of
ERROR_INVALID_PARAMETER :
raise EIpHlpError.CreateFmt(sInvalidParameter, [FunctionName]);
ERROR_NO_DATA :
raise EIpHlpError.CreateFmt(sNoData, [FunctionName]);
ERROR_NOT_SUPPORTED :
raise EIpHlpError.CreateFmt(sNotSupported, [FunctionName]);
else ;
RaiseLastWin32Error;
end;
end;
procedure VVGetTcpTable(var pTcpTable: PTMibTCPTable; var dwSize: DWORD;
const bOrder: BOOL);
var
Res: DWORD;
begin
pTcpTable := Nil;
dwSize := 0;
if @GetTcpTable = Nil then
raise EIpHlpError.CreateFmt(sNotImplemented, ['GetTcpTable']);
Res := GetTcpTable(pTcpTable, dwSize, bOrder);
if Res = ERROR_INSUFFICIENT_BUFFER then
begin
Getmem(pTcpTable, dwSize);
FillChar(pTcpTable^, dwSize, #0);
Res := GetTcpTable(pTcpTable, dwSize, bOrder);
end;
if Res <> NO_ERROR then
IpHlpError('GetTcpTable', Res);
end;
function GetRemoteTcpAddr(AddrPort: String): String;
var
pTcpTable: PTMibTCPTable;
dwSize: DWORD;
i: integer;
RemoteAddrPort: String;
Function GetTcpPortNumber(aDWord: DWord): Longint;
begin
Result := Trunc(aDWord / 256 + (aDWord Mod 256) * 256);
End;
begin
VVGetTcpTable(pTcpTable, dwSize, False);
if pTcpTable <> nil then
try
for i := 0 to pTcpTable^.dwNumEntries do
with pTcpTable^.table do
begin
RemoteAddrPort := IpAddressToString(dwRemoteAddr)+':'+
Inttostr(GetTcpPortNumber(dwRemotePort));
if (RemoteAddrPort = AddrPort) and (dwState=5) then
begin
Result := IpAddressToString(dwLocalAddr);
end;
end;
finally
Freemem(pTcpTable);
end;
end;
end.
-----------------------------------------------------
详细请我的笔记:
使用Ip Helper API得到当前系统的Tcp所有打开端口及IP地址
http://www.delphibbs.com/keylife/iblog_show.asp?xid=2942