使用 C# 获取应用程序打开的所有 TCP 连接

发布于 2024-12-01 06:37:59 字数 56 浏览 1 评论 0原文

如何查找特定应用程序打开的所有 tcp 连接,可能是通过进程 ID 或 smth。像这样?我用C#

How to find all tcp connections opened by specific application, maybe by process id or smth. like this? I use C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄情伤 2024-12-08 06:37:59

该项目可以帮助您:获取活动的 TCP/UDP 连接

This project can help you :Getting active TCP/UDP connections

故人如初 2024-12-08 06:37:59

下面是一个简单的代码片段,用于获取所有连接及其 PID:

using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.InteropServices;

public static class TcpHelper {

    [DllImport("iphlpapi.dll", SetLastError = true)]
    private static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, int ipVersion, int tcpTableType, int reserved);

    [StructLayout(LayoutKind.Sequential)]
    public struct TcpRow {
        public uint state;
        private uint localAddr;
        private byte localPort1, localPort2, localPort3, localPort4;
        private uint remoteAddr;
        private byte remotePort1, remotePort2, remotePort3, remotePort4;
        public int owningPid;
        public string LocalAddress {get {return new IPAddress(localAddr).ToString();}}
        public string RemoteAddress {get {return new IPAddress(remoteAddr).ToString();}}
        public int LocalPort  {get {return (localPort1 << 8) + localPort2;}}
        public int RemotePort {get {return (remotePort1 << 8) + remotePort2;}}
    }

    public static List<TcpRow> GetExtendedTcpTable() {
        List<TcpRow> tcpRows = new List<TcpRow>();

        IntPtr tcpTablePtr = IntPtr.Zero;
        try {
            int tcpTableLength = 0;
            if (GetExtendedTcpTable(tcpTablePtr, ref tcpTableLength, false, 2, 5, 0) != 0) {
                tcpTablePtr = Marshal.AllocHGlobal(tcpTableLength);
                if (GetExtendedTcpTable(tcpTablePtr, ref tcpTableLength, false, 2, 5, 0) == 0) {
                    TcpRow tcpRow = new TcpRow();
                    IntPtr currentPtr = tcpTablePtr + Marshal.SizeOf(typeof(uint));

                    for (int i = 0; i < tcpTableLength / Marshal.SizeOf(typeof(TcpRow)); i++) {
                        tcpRow = (TcpRow)Marshal.PtrToStructure(currentPtr, typeof(TcpRow));
                        if (tcpRow.RemoteAddress != "0.0.0.0") {tcpRows.Add(tcpRow);}
                        currentPtr += Marshal.SizeOf(typeof(TcpRow));
                    }
                }
            }
        }
        finally {
            if (tcpTablePtr != IntPtr.Zero) {
                Marshal.FreeHGlobal(tcpTablePtr);
            }
        }
        return tcpRows;
    }
}

Here is an easy code-snippet to get all connections with their PIDs:

using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.InteropServices;

public static class TcpHelper {

    [DllImport("iphlpapi.dll", SetLastError = true)]
    private static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, int ipVersion, int tcpTableType, int reserved);

    [StructLayout(LayoutKind.Sequential)]
    public struct TcpRow {
        public uint state;
        private uint localAddr;
        private byte localPort1, localPort2, localPort3, localPort4;
        private uint remoteAddr;
        private byte remotePort1, remotePort2, remotePort3, remotePort4;
        public int owningPid;
        public string LocalAddress {get {return new IPAddress(localAddr).ToString();}}
        public string RemoteAddress {get {return new IPAddress(remoteAddr).ToString();}}
        public int LocalPort  {get {return (localPort1 << 8) + localPort2;}}
        public int RemotePort {get {return (remotePort1 << 8) + remotePort2;}}
    }

    public static List<TcpRow> GetExtendedTcpTable() {
        List<TcpRow> tcpRows = new List<TcpRow>();

        IntPtr tcpTablePtr = IntPtr.Zero;
        try {
            int tcpTableLength = 0;
            if (GetExtendedTcpTable(tcpTablePtr, ref tcpTableLength, false, 2, 5, 0) != 0) {
                tcpTablePtr = Marshal.AllocHGlobal(tcpTableLength);
                if (GetExtendedTcpTable(tcpTablePtr, ref tcpTableLength, false, 2, 5, 0) == 0) {
                    TcpRow tcpRow = new TcpRow();
                    IntPtr currentPtr = tcpTablePtr + Marshal.SizeOf(typeof(uint));

                    for (int i = 0; i < tcpTableLength / Marshal.SizeOf(typeof(TcpRow)); i++) {
                        tcpRow = (TcpRow)Marshal.PtrToStructure(currentPtr, typeof(TcpRow));
                        if (tcpRow.RemoteAddress != "0.0.0.0") {tcpRows.Add(tcpRow);}
                        currentPtr += Marshal.SizeOf(typeof(TcpRow));
                    }
                }
            }
        }
        finally {
            if (tcpTablePtr != IntPtr.Zero) {
                Marshal.FreeHGlobal(tcpTablePtr);
            }
        }
        return tcpRows;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文