确定哪个网络适配器的方法是使用 [英] Determine which network adapter a process is using

查看:131
本文介绍了确定哪个网络适配器的方法是使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了谷歌的几个类似的问题,但没有完全匹配什么,我试图做的。我正在一个滞后还原程序(游戏),基本上降低了用户的MTU当一定的过程是开放的,并且当处理被关闭恢复它。然而,MTU是网络适配器特定的设置,有些用户有多个连接的网络适配器。为此,我认为这会是不错的方案也检测正在使用的适配器的游戏,只有改变MTU的适配器。

I've seen several similar questions on Google, but nothing exactly matches what I'm trying to do. I'm making a lag-reducing program (for a game) that basically lowers the user's MTU when a certain process is open, and restores it when the process is closed. However, MTU is a network-adapter specific setting, and some users have multiple connected network adapters. To this end, I thought it'd be nice to have the program also detect which adapter is being used by the game, and only change the MTU on that adapter.

本场比赛将只使用的有一个的适配器一次。

The game will only use one adapter at a time.

我无法在终端服务器的IP地址硬编码,因为他们改变相当频繁。这似乎是有必须是一种方法来确定哪个适配器其他进程正在使用不知道最终的IP地址,但我似乎无法找到它。

I can't hardcode in end-server-IP addresses because they change fairly frequently. It seems to be there must be a way to determine which adapter the other process is using without knowing the end IP address, but I can't seem to find it.

修改
由于蝉及REMCO,我已经解决了这个问题。

EDIT: Thanks to Cicada and Remco, I've solved the problem.

我用在ManagedIPHelper类REMCO挂( ManagedIpHelper )和蝉的评论使我这个文章(确定在.NET活动的网络接口

I used the ManagedIPHelper class that Remco linked to (ManagedIpHelper) and Cicada's comments led me to this article (Identifying active network interface in .NET)

结合的一些(讨厌,可怕未优化)LINQ,我得到这个代码片断,这需要进程名,并返回它使用的网络接口或null如果不能找到一个。

Combining those with some (Nasty, horribly unoptimized) LINQ, I got this code snippet, which takes the process name and returns the Network Interface it's using, or null if it can't find one.

    private NetworkInterface getAdapterUsedByProcess(string pName)
    {
        Process[] candidates = Process.GetProcessesByName(pName);
        if (candidates.Length == 0)
            throw new Exception("Cannot find any running processes with the name " + pName + ".exe");

        IPAddress localAddr = null;
        using (Process p = candidates[0])
        {
            TcpTable table = ManagedIpHelper.GetExtendedTcpTable(true);
            foreach (TcpRow r in table)
                if (r.ProcessId == p.Id)
                {
                    localAddr = r.LocalEndPoint.Address;
                    break;
                }
        }

        if (localAddr == null)
            throw new Exception("No routing information for " + pName + ".exe found.");

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            IPInterfaceProperties ipProps = nic.GetIPProperties();
            if (ipProps.UnicastAddresses.Any(new Func<UnicastIPAddressInformation, bool>((u) => { return u.Address.ToString() == localAddr.ToString(); })))
                return nic;
        }
        return null;
    }



测试证实了这一工作的非常的!非常感谢,你们

使用这个片段旁注任何人!


  • 您将需要ManagedIpHelper类。

  • 您的应用程序可能需要请求抬高,视情况而定。

  • 多个正在运行的进程(个人认为铬)将返回一个不确定的结果。如果你要使用此代码与multpile - 过程 - candiate的情况下,我的的建议你改变的使用(进程p =考生[0])以更特定的过滤器,即基于PID。

  • 您可能还需要impliment新的异常类型,这样你就可以,例如,搭上没有路由信息更干净,原因是这错误往往是只是在等待一个位(让目标进程打开的连接),然后重试修复。

  • You'll need the ManagedIpHelper classes.
  • Your app may need to request elevation, depending on the situation.
  • Multiple running processes (think Chrome) will return an undefined result. If you're going to use this code with a multpile-process-candiate situation, I highly recommend you change using (Process p = candidates[0]) to a more specific filter, ie based on PID.
  • You may also want to impliment new exception types, so you can, for example, catch "No routing info" more cleanly, the reason being that this error is often fixed by simply waiting a bit (to let the target process open a connection) and then retrying.

推荐答案

除了蝉,这一点必须帮助您:

in addition to Cicada, this must help you:

这是周围的一些C / C ++代码C#的包装,它可以让你所有的列表打开连接关联的PID(进程ID)。

It is a C# wrapper around some c/c++ code, which gets you the list of all open connections with associated PID ( Process Id ).

http://www.timvw.be/2007/09/09/build-your-own-netstatexe-with-c/

我不相信这是去的,基于可执行文件的路径/名称确定进程(ID),并设法找到该进程的当前连接的唯一方式。

I do believe this is the only way to go, determine the process(id) based on executable path/name and try to find the current connection of that process.

这篇关于确定哪个网络适配器的方法是使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆