sys_windows.go 492 B

123456789101112131415161718192021222324252627282930
  1. //go:build windows
  2. // +build windows
  3. package sys
  4. import (
  5. "errors"
  6. "github.com/shirou/gopsutil/v4/net"
  7. )
  8. func GetConnectionCount(proto string) (int, error) {
  9. if proto != "tcp" && proto != "udp" {
  10. return 0, errors.New("invalid protocol")
  11. }
  12. stats, err := net.Connections(proto)
  13. if err != nil {
  14. return 0, err
  15. }
  16. return len(stats), nil
  17. }
  18. func GetTCPCount() (int, error) {
  19. return GetConnectionCount("tcp")
  20. }
  21. func GetUDPCount() (int, error) {
  22. return GetConnectionCount("udp")
  23. }