Vincent
Vincent

Reputation: 3529

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 :)

Upvotes: 9

Views: 6293

Answers (1)

dtb
dtb

Reputation: 217411

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;

Upvotes: 12

Related Questions