?
Solved

Discount connection

Posted on 2003-03-15
5
Medium Priority
?
578 Views
Last Modified: 2010-04-04
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
Comment
Question by:S_Warrior
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:steve_hsk
ID: 8145946
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
 

Author Comment

by:S_Warrior
ID: 8147200
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
 

Expert Comment

by:CleanupPing
ID: 9316763
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10639928
PAQed, with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question