views:

91

answers:

1

Dear Devs

I would like to write an application that tells when a tcp port opened and closed on windows platform please if some can give me piece of code or sample

my preference is C# code.

Regards

+2  A: 

Assuming you want to monitor TCP Sockets on the machine, you can use System.Net.NetworkInformation.GetActiveTcpConnections which should give you a list of TCP connections and the current state of each. Which you can call on a regular timer to get updated states of the sockets.

I am more familiar with the Win32 APIs for this, such as GetTcpTable and the less documented GetTcpTableEx, the later also provides the process ID of the process that ownes the local end point. So the above might not give exactly what I believe it does :)

Of course if you need to be notified the very instant before or after a tcp socket is created or it's state changes, that unfortunately would require a kernel mode driver which cannot be written in .NET.

Chris Taylor
I know that `netstat -b` requires elevated privileges, so if `GetTcpTableEx(...)` provides the same functionality, then it might also require admin rights.
Allon Guralnek