怎么获得当前正在活动的连接的IP地址(300分)

  • 主题发起人 主题发起人 datm
  • 开始时间 开始时间
D

datm

Unregistered / Unconfirmed
GUEST, unregistred user!
我用WinSock编程做了一个服务器端,当有一个客户端连接上来了我想得到客户端是通过那个网卡连接上来,假设两个网卡的IP不一样,请问当前客户端是用那个IP地址连上来的?
先谢了
300分不够再加
 
帮你提前。
 
用ASP脚本可以记录客户端来显示IP地址。
 
netstat -a -n
 
一楼的谢谢
二楼的不知道说什么
三楼的谢谢。我怎么用编程实现?
 
用“ Delphi ”写可用“Socket.RemoteAddress”获得对方IP,
 
你可以列举当前所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


 
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
IP:=Socket.RemoteAddress;
end;
 
谢谢wjh_wy 260分送上
lich 40分
其他的答非所问

我用GetSockName实现了(必须在进行通讯以后才行)
 
后退
顶部