Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a number of open sockets in C#?

I'm trying to log the number of open sockets in my C# server application. Like the info you could get from "netstat -s":

TCP Statistics for IPv4

  Active Opens                        = 22765
  Passive Opens                       = 9316

I could parse the result from "netstat".. but if there's a class for it, then why would I..

By the way, with the help of class System.Diagnostics.Process, I could log a number of handles, and threads. I thought that a socket is a handle, but it seems not.. In my situation, the # of handles was way below # of active connections from netstat.

Any suggestions, advices or answers will be greatly appreciated. Thank you :)

like image 763
Vincent Avatar asked Oct 10 '10 16:10

Vincent


1 Answers

Have a look at the TcpStatistics Class.

For example, the TcpStatistics.CurrentConnections Property returns "The number of TCP connections for which the current state is either ESTABLISHED or CLOSE-WAIT."

long result = IPGlobalProperties.GetIPGlobalProperties()
                                .GetTcpIPv4Statistics()
                                .CurrentConnections;
like image 90
dtb Avatar answered Nov 02 '22 13:11

dtb