douhuan1497 2017-10-10 19:50
浏览 122

Golang:替代C#(。Net)IPGlobalProperties.GetIPGlobalProperties()

I am rewriting one of my windows service(C#) in Golang. I have almost figured out and rewrote the code in Go but got stuck at one place where I am not been able to figure out golang alternative.

public static int GetNumberOfLocalEstablishedConnectionsByPort(string IPAddress, int Port)
    {
        int Result = 0;
        IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
        TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();

        foreach (TcpConnectionInformation tcpInfo in tcpConnections)
        {
                if (tcpInfo.State == TcpState.Established && tcpInfo.LocalEndPoint.Port == Port) 
                {
                            Result++;
                }
        }

        return Result;
    }

Basically in this method I am finding out active number of connections based on a IP address and Port.

I am seeking help in order to find out how can I rewrite this C# code into Golang. I am using windows OS and want a solution based on Windows OS

  • 写回答

1条回答 默认 最新

  • doumu9799 2017-10-28 07:59
    关注

    In order to find the number of active connections, there is no equivalent wrapper similar to above-mentioned C# methods in Golang.

    Though we can achieve this using the syscalls for this we can use the following command if you are Linux user.

    netstat -anp | grep :80 | grep ESTABLISHED | wc -l
    

    However, in windows os, you might face some problem because grep and wc (word count) command will not work. I faced this problem when I ran it as a windows service.

    For Windows OS the following command worked.

    netstat -nt | findstr :80  | findstr ESTABLISHED | find /v /c ""
    

    For /v /c to work you might need to make the exe file execute as administrator .

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制