• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

Discount connection

Hello every one

I am looking for a way to discount an active connection(like u see in netStat).

a good API function will be the best (-/+)
0
S_Warrior
Asked:
S_Warrior
1 Solution
 
steve_hskCommented:
Hi Warrior ...

There are two ways to grab the current active ports list that I know of. The first is using SNMP, and launching a GetRequest to the Window's MIB. This can be a little clumsy, and not all machines have the SNMP MIB installed.

The second method is using the IP Helper API : (IpHlpApi.DLL, IpHlpApi.Lib, IpHlpApi.Pas, IpTypes.Pas, IpExport.pas, IpRtrMib.pas, IPIfCons.pas)

This is a standard Windows NT4, 2000, XP API and the Delphi Libraries can be downloaded from the following link, together with an Active Connection TCP & UDP Logger example app with source code :-

http://users.pandora.be/dirk.claessens2/software.htm

The IPHlpApi allows us programmatically to undertake the tasks that the DOS programs NETSTAT, ARP, IPCONFIG, ROUTE that are available on your PC now. Try them in a DOS window.

This Link nicely explains how to go about re-creating these utilities :
http://www.netti.hu/doc/delphi_zine/IP%20Helper%20API%20Part%20I.htm

Look for the Fnugry Netstat Components for another example :
http://www.torry.net/networks.htm

And just for Reference the following code should do exactly what you need :

function IpAddrToString(Addr, Port: DWORD; Local: Boolean): string; overload;
var
  inad: in_addr;
  HostEnt: PHostEnt;
  ServEnt: PServEnt;
begin
  inad.s_addr := Addr;
  Result := inet_ntoa(inad);
  if ResolveNames then
  begin
    if Local or (Addr = 0) then
    begin
      ServEnt := GetServByPort(Port, nil);
      if ServEnt <> nil then
        Result := GetLocalComputerName + ':' + ServEnt^.s_name + '(' + ServEnt^.s_proto + ')'
      else
        Result := GetLocalComputerName + ':' + IntToStr(htons(Port));
    end
    else
    begin
      HostEnt := GetHostByAddr(PChar(@Addr), SizeOf(DWORD), AF_INET);
      if HostEnt <> nil then
        Result := HostEnt^.h_name + ':' + IntToStr(htons(Port))
      else
        Result := Result + ':' + IntToStr(htons(Port));
    end;
  end;
end;

function TcpStateString(State: DWORD): string;
begin
  case State of
    MIB_TCP_STATE_CLOSED:    Result := 'Closed';
    MIB_TCP_STATE_LISTEN:    Result := 'Listening';
    MIB_TCP_STATE_SYN_SENT:  Result := 'Syn sent';
    MIB_TCP_STATE_SYN_RCVD:  Result := 'Syn received';
    MIB_TCP_STATE_ESTAB:     Result := 'Established';
    MIB_TCP_STATE_FIN_WAIT1: Result := 'Fin wait1';
    MIB_TCP_STATE_FIN_WAIT2: Result := 'Fin wait2';
    MIB_TCP_STATE_CLOSE_WAIT:Result := 'Close wait';
    MIB_TCP_STATE_CLOSING:   Result := 'Closing';
    MIB_TCP_STATE_LAST_ACK:  Result := 'Last ack';
    MIB_TCP_STATE_TIME_WAIT: Result := 'Time wait';
    MIB_TCP_STATE_DELETE_TCB:Result := 'Delete TCB';
  else
    Result := 'Unknown';
  end;
end;

procedure DisplayTcpConnections;
var
  Size: ULONG;
  TcpTable: PMibTcpTable;
  TcpRow: TMibTcpRow;
  I: Integer;
begin
  Size := 0;
  if GetTcpTable(nil, Size, True) <> ERROR_BUFFER_OVERFLOW then Exit;
  TcpTable := AllocMem(Size);
  try
    if GetTcpTable(TcpTable, Size, True) = NO_ERROR then
    begin
      for I := 0 to TcpTable^.dwNumEntries - 1 do
      begin
        {$R-}TcpRow := TcpTable^.Table[I];{$R+}
        WriteLn(Format('  %-5s  %-25s  %-25s  %-s',
         ['TCP',
          IpAddrToString(TcpRow.dwLocalAddr, TcpRow.dwLocalPort, True),
          IpAddrToString(TcpRow.dwRemoteAddr, TcpRow.dwRemotePort, False),
          TcpStateString(TcpRow.dwState)]));
      end;
    end;
  finally
    FreeMem(TcpTable);
  end;
end;

procedure DisplayUdpConnections;
var
  Size: ULONG;
  I: Integer;
  UdpTable: PMibUdpTable;
  UdpRow: TMibUdpRow;
begin
  Size := 0;
  if GetUdpTable(nil, Size, True) <> ERROR_BUFFER_OVERFLOW then Exit;
  UdpTable := AllocMem(Size);
  try
    if GetUdpTable(UdpTable, Size, True) = NO_ERROR then
    begin
      for I := 0 to UdpTable.dwNumEntries - 1 do
      begin
        {$R-}UdpRow := UdpTable.Table[I];{$R+}
        WriteLn(Format('  %-5s  %-25s  %-25s  %-s',
         ['UDP',
          IpAddrToString(UdpRow.dwLocalAddr, UdpRow.dwLocalPort, True),
          '*.*',
          '']));
      end;
    end;
  finally
    FreeMem(UdpTable);
  end;
end;

This code was written by Vladimir Vassiliev - voldemarv@hotpop.com

If you need any further clues, let us know !
Hope this helps,
STeve
0
 
S_WarriorAuthor Commented:
steve_hsk, I am sorry but I didnt found any example that will discount an active connection,

I dont want to see active connections I want to kill them, kill them all :)

0
 
CleanupPingCommented:
S_Warrior:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
moduloCommented:
PAQed, with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now