I'm trying to find out how many sockets are currently being used on my machine. If it's possible, I need an API that returns the exact number of sockets being used at any time.
I have tried TCPView, and the command netstat
for many version, but all I get is a list of connections.
CodePudding user response:
If you are just interested in the number of TCP/UDP sockets in use, have a look at the following APIs:
GetTcpStatistics()
, GetTcpStatisticsEx()
, GetTcpStatisticsEx2()
GetUdpStatistics()
, GetUdpStatisticsEx()
, GetUdpStatisticsEx2()
But those don't really tell you anything useful, and the stats are global, not per-interface. There are other APIs that can enumerate the available sockets and retrieve actual details about them (statuses, port numbers, etc), which you can use to fine-tune your searching as needed (eg, ESTABLISHED on IP x Port y):
GetTcpTable()
, GetTcpTable2()
, GetExtendedTcpTable()
GetUdpTable()
, GetUdp6Table()
, GetExtendedUdpTable()
CodePudding user response:
Just the amount?
netstat -a -n | find /i "127.0.0.1" | find /I "ESTABLISHED" > t.txt && powershell -command "& Get-Content "t.txt" | Measure-Object -Line"
Replace 127.0.0.1 if needed. On my box it resulted in
Lines Words Characters Property
----- ----- ---------- --------
24
Looking at t.txt, it will will show something like:
TCP 127.0.0.1:49672 127.0.0.1:49673 ESTABLISHED
TCP 127.0.0.1:49673 127.0.0.1:49672 ESTABLISHED
TCP 127.0.0.1:49674 127.0.0.1:49675 ESTABLISHED
TCP 127.0.0.1:49675 127.0.0.1:49674 ESTABLISHED
TCP 127.0.0.1:57354 127.0.0.1:57355 ESTABLISHED
TCP 127.0.0.1:57355 127.0.0.1:57354 ESTABLISHED
TCP 127.0.0.1:57356 127.0.0.1:57357 ESTABLISHED
TCP 127.0.0.1:57357 127.0.0.1:57356 ESTABLISHED