Sometimes developers may need to monitor network connections by running a machine programmatically. How to list down the network connections between local and remote addresses, ports, protocol, status, etc.? Don’t know what to do. Don’t worry. MiTec’s System Information Management Suite’s component helps to identify easily. We will learn how to use the thread TNetConMonThread to demonstrate the established connections by a process in this post.
Platforms: Windows.
Installation Steps:
You can easily install this Component Suite from GetIt Package Manager. The steps are as follows.
- Navigate In RAD Studio IDE->Tools->GetIt Package Manager->select Components in Categories->Components->Trail -MiTec system Information Component Suite 14.3 and click Install Button.
- Read the license and Click Agree All. An Information dialog saying ‘Requires a restart of RAD studio at the end of the process. Do you want to proceed? click yes and continue.
- It will download the plugin and installs it. Once installed Click Restart now.
How to run the Demo app:
- Navigate to the System Information Management Suite trails setup, Demos folder which is installed during Get It installation e.g) C:UsersDocumentsEmbarcaderoStudio21.0CatalogRepositoryMiTeC-14.3DemosDelphi20
- Open the NetCon project in RAD studio 10.4.1 compile and Run the application.
- This Demo App shows how to list down network connections by process in your machine and show the connection record properties.
Components used in MSIC NetCon Demo App:
- TNetConMonThread: Monitors network connections by processes in real-time and provides their properties. This class is singleton – it means it ensures to be instantiated only once per application.
- TListView for viewing network connections between peers.
Implementation Details:
- An instance FNCMof TNetConMonThread is created. And in regular interval of thread the Network connections list is refreshed and updated to the screen using the OnInterval Event handler.
- On each interval, the Network connections List( using TNetConRecord) is added to the list view by clearing the existing list and looping the RecordCount.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
procedure TwndMain.RefreshList; var i,c: Integer; vp: Integer; SInfo: TScrollInfo; r: TRect; p: TNetConRecord; s: string; vi: TVersionInfo; begin SInfo.cbSize:=SizeOf(SInfo); SInfo.fMask:=SIF_ALL; GetScrollInfo(List.Handle,SB_VERT,SInfo); vp:=SInfo.nPos; List.Items.BeginUpdate; try List.Items.Clear; c:=FNCM.RecordCount; for i:=0 to c-1 do begin FNCM.GetRecord(i,p); p.ProcessName:=GetProcessName(p.PID); FNCM.SetProcess(i,p.ProcessName,vi,-1); with List.Items.Add do begin Caption:=Format('%s (%d)',[p.ProcessName,p.PID]); if not IsAddressNull(p.LocalAddress) then SubItems.Add(p.LocalAddressString) else SubItems.Add(MachineName); if p.LocalPort>0 then SubItems.Add(IntToStr(p.LocalPort)) else SubItems.Add(''); if not IsAddressNull(p.RemoteAddress) then SubItems.Add(p.RemoteAddressString) else if SameText('UDP',p.Protocol) then SubItems.Add('') else SubItems.Add(MachineName); if p.RemotePort>0 then SubItems.Add(IntToStr(p.RemotePort)) else SubItems.Add(''); s:=p.Protocol; if p.Typ=AF_INET6 then s:=s+'6'; SubItems.Add(s); if SameText('TCP',p.Protocol) then case p.State of MIB_TCP_STATE_CLOSED: SubItems.Add('Closed'); MIB_TCP_STATE_LISTEN: SubItems.Add('Listen'); MIB_TCP_STATE_SYN_SENT: SubItems.Add('SYN sent'); MIB_TCP_STATE_SYN_RCVD: SubItems.Add('SYN received'); MIB_TCP_STATE_ESTAB: SubItems.Add('Established'); MIB_TCP_STATE_FIN_WAIT1: SubItems.Add('FIN wait 1'); MIB_TCP_STATE_FIN_WAIT2: SubItems.Add('FIN wait 2'); MIB_TCP_STATE_CLOSE_WAIT: SubItems.Add('Close wait'); MIB_TCP_STATE_CLOSING: SubItems.Add('Closing'); MIB_TCP_STATE_LAST_ACK: SubItems.Add('Last ACK'); MIB_TCP_STATE_TIME_WAIT: SubItems.Add('Time wait'); MIB_TCP_STATE_DELETE_TCB: SubItems.Add('Delete TCB'); else SubItems.Add('Unknown'); end else SubItems.Add(''); end; end; finally List.Items.EndUpdate; end; if List.Items.Count>0 then begin List.Items.BeginUpdate; try r:=List.Items[0].DisplayRect(drBounds); List.Scroll(0,vp*(r.Bottom-r.Top)); finally List.Items.EndUpdate; end; end; sb.Panels[0].Text:=Format('%d item(s)',[List.Items.Count]); end; |
It’s that simple to monitor network connections established in your machine and view the details of the connections. Use this MiTeC component suite and get the job done with less code.
With the use of a Windows IDE, you can easily identify network connections by process in your Windows apps with a component suite. Try your Free Trial here.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition