English 中文(简体)
TCP Connection Creation and Closing Event Hooking [closed]
原标题:
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

What helper classes C# provides to monitor all TCP connection on an OS. A piece of Sample code would also be appreciated.

Note: Original intent was to monitor this on Windows but would be nice to do the same on other os using .Net Core.

最佳回答

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.

问题回答

If you really need an event based mechanism, event trace for windows is the way to go. Since you said it s only a preference :) you re best off writing this using the windows api (c/c++).

http://msdn.microsoft.com/en-us/library/aa964766(v=vs.85).aspx

From .NET 3.5 onwards parts of the event trace are accessible via .NET using the System.Diagnostics.Eventing namespace. I understand that this has been improved in .NET 4.0. I haven t looked into this myself so I don t know whether TCP Connection detection is possible.

I know for sure that you can do what you want with windows api and ETW.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签