CSDN問答:如何查看本機有哪些連接埠被開啟

來源:互聯網
上載者:User

獲得當前系統的Tcp所有開啟連接埠及IP地址
提交日期:2003-11-22 作者:cjsh
 
關鍵詞:TCP 通訊埠 IP地址 Ip Helper API 
//GetTcpTable函數單元
unit untIPHLPAPI;

interface

uses
Windows, sysutils, WinSock;

type
EIpHlpError = class(Exception);
//----------------TCP結構------------------------------------------------
PTMibTCPRow = ^TMibTCPRow;
TMibTCPRow = packed record
dwState : DWORD;//狀態
dwLocalAddr : DWORD;//本地IP地址
dwLocalPort : DWORD;//本地連接埠號碼
dwRemoteAddr: DWORD;//遠程IP地址
dwRemotePort: DWORD;//遠程連接埠號碼
end;
//
PTMibTCPTable = ^TMibTCPTable;
TMibTCPTable = packed record
dwNumEntries : DWORD; //Tcp開啟的數量
Table : array[0..0] of TMibTCPRow;
end;

//------------------從IPHLPAPI.DLL輸入的API函數----------------------------
function GetTcpTable(pTcpTable: PTMibTCPTable; var pdwSize: DWORD;
bOrder: BOOL): DWORD; stdcall;

//轉換成IP地址形式
function IpAddressToString(Addr: DWORD): string;
//GetTcpTable的實現過程
procedure VVGetTcpTable(var pTcpTable: PTMibTCPTable; var dwSize: DWORD;
const bOrder: BOOL);
//轉換成連接埠號碼
Function GetTcpPortNumber(aDWord: DWord): Longint;

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 GetTcpPortNumber(aDWord: DWord): Longint;
begin
Result := Trunc(aDWord / 256 + (aDWord Mod 256) * 256);
end;

end.

//示範例子
procedure TFmIpTest.BtGetTcpTableClick(Sender: TObject);
var
pTcpTable: PMibTcpTable;
dwSize: DWORD;
i: integer;
begin
Memo1.Lines.Add(‘GetTcpTable‘);
VVGetTcpTable(pTcpTable, dwSize, False);
if pTcpTable <> nil then
try
Memo1.Lines.Add(‘ NumEntries: ‘ + IntToStr(pTcpTable^.dwNumEntries));
Memo1.Lines.Add(‘ Local Address Port Remote Address Port State‘);
for i := 0 to pTcpTable^.dwNumEntries do
with pTcpTable^.table[i], Memo1.Lines do
begin
Add(Format(‘ %15s %5d %15s %5d %5d‘, [IpAddressToString(dwLocalAddr),
GetTcpPortNumber(dwLocalPort), IpAddressToString(dwRemoteAddr),
GetTcpPortNumber(dwRemotePort), dwState]));
end;
finally
Freemem(pTcpTable);
end;
end;

http://nssoft.net/showdoc.asp?did=843

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.