有关远程 IP 地址的信息
我想跟踪与我的计算机通信的所有 IP 地址。我正在使用 C# 和 Visual Studio。有一个列表框,我将在其中放置所有远程 IP 地址。我是网络编程新手,所以我需要有关此类问题的一些指导。
我的问题是如何做到这一点?如何获取我的计算机连接的远程 IP 地址的信息?
I want to track all IP addresses with which my computer is communicating. I am using C# and Visual Studio. There is one listBox in which I will put all remote IP address. I am new to network programming so I need some direction about that kind of problem.
My question is how to do this? How to get information about remote IP addresses which which my computer make connections?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以利用
netstat
命令获取有关您的计算机正在与哪些 IP 地址和端口进行通信的信息。您可以定期运行此命令并解析文本输出。例如,以下命令将列出所有连接的状态,包括本地和远程连接详细信息:
或者,如果您具有管理员权限,您也可以获得可执行详细信息:
您可以使用 System.Diagnostics.Process 并解析文本输出,例如:
http://msdn.microsoft.com/en-us/library/system.diagnostics .process.standardoutput.aspx
You could leverage the
netstat
command to get information about which IP addresses and ports your machine is communicating with. You could run this command on a regular basis and parse the text output.For example, the following command will list the state of all connections including local and remote connection details:
Or if you have admin privilleges you can get executable details too:
You can call this command from .Net using System.Diagnostics.Process and parse the text output, e.g:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
大多数套接字编程应用程序都与操作系统之间大致标准化的 TCP 套接字 API 进行通信。如果您只想编写一个简单的服务器并可视化客户端与在您的进程中运行的服务器的通信,那非常简单。但是,您的进程无法看到使用典型套接字 API 与计算机上的其他进程通信的套接字连接。
如果您想访问整个机器正在通话的所有 IP,则必须使用较低级别的 API。这在操作系统之间没有标准化。对于 Windows,我会查看 Winsock API,看看您是否可以找到如何查看操作系统的 tcp/ip 堆栈。它变得更加复杂,因为 TCP 连接建立在 IP 数据包之上。你想看什么水平?只是 TCP,或所有同样基于 IP 的协议。 ARP、UDP、TCP、ICMP 列出了标准的视图。
如果您只是在学习,那么只需做一个简单的服务器,并且在您的进程中与该服务器通信的所有连接都会简单得多。走低将很快跳入深渊。我并不是说你不应该这样做。您想保持简单并保留与您的进程正在对话的连接,还是想进入深邃黑暗的 Window 森林?选择你的冒险。
编辑:较低级别访问的更好选择是带有 C# 包装器的 WinPcap。这将使您获得原始数据包信息。甚至可以让您将 PC 置于混杂模式并嗅探子网上的所有流量,而不仅仅是您的 PC。
Most socket programming applications talk to the TCP socket API that's roughly standardized between operating systems. If you just want to write a simple server and visualize the clients talking to your server running in your process that's pretty easy. However, your process can't see the socket connections talking to other processes on your machine with the typical socket API.
If you want to get access to all IPs the entire machine is talking too you'll have to use a lower level API. That's not standardized between OS. For Windows I'd look at Winsock API to see if you can find how to look at the tcp/ip stack of the OS. It gets more complex because TCP connections ride on top of IP packets. At what level do you want to look at? Just TCP, or all protocols that also ride on top of IP. ARP, UDP, TCP, ICMP to name a view standard ones.
If you're just learning then doing just a simple server, and all the connections talking to that server in your process is much simpler. Going lower is going to be jumping into the deep end quickly. Not that I'm saying you shouldn't this. Would you like to keep it simple and stay with the connections your process is talking to, or would you like to go into the deep dark Window's forest? Choose your adventure.
Edit: Better option for lower level access is WinPcap with a C# wrapper. That will let you get the raw packet information. Even let you put your PC in promiscuous mode and sniff all traffic on your subnet not just your PC.
在 Windows 上,有一个 IP 帮助程序库 iphlpapi< /a>,它可以发现有关计算机上打开的连接的信息。特别是,您需要查看
GetTcpTable
和GetUdpTable
函数。问题是这是一个 C API。要从 C# 使用这些,需要使用 P/Invoke API。这是一项微不足道的任务,但可能需要一些时间才能完成。使用的每个 C 结构都需要在 C# 中重写,利用 System.Runtime.InteropServices 命名空间来编组它们。其中大部分可以自动化,或者通过使用正确的属性装饰结构字段来完成,但在某些情况下,您可能需要手动封送结构。
您导入的 C 函数需要使用
[DllImport("iphlpapi.dll")]
进行标记,并根据您要导入的函数使用任何其他相关属性。您可以在 msdn 阅读有关 PInvoke 的更多信息,也许还可以找到一些已经编写的内容在 pinvoke wiki 导入。此外,还有一个工具可以尝试为您自动化大部分操作,即 Pinvoke interop 助手< /a>On Windows, there's an IP helper library, iphlpapi, which can discover information about open connections on your machine. In particular, you want to look at the
GetTcpTable
andGetUdpTable
functions.The problem is this is a C API. To use these from C# requires you to use the P/Invoke API. It's a trivial task, but can take some time to do. Each of the C structs used need rewriting in C#, making use of the
System.Runtime.InteropServices
namespace to marshal them. Much of this can be automated, or done by decorating struct fields with the correct attributes, but there may be cases where you need to marshal a structure manually.The C functions you import need marking with
[DllImport("iphlpapi.dll")]
with any other relevant attributes depending on the function you're importing. You can read more about PInvoke at msdn, and perhaps find some already written imports at the pinvoke wiki. In addition, there is a tool which can attempt to automate much of it for you, the Pinvoke interop assitant假设您有
ObservableCollection; mAccessInfo
在代码中的某个位置作为绑定到列表的公开可见变量,您可以将其放置在侦听连接的代码部分中:由于它是 ObservableCollection,因此在进入列表的每个新连接上,列表你一定会更新。
编辑:正如 Cody Gray 指出的那样,如果您的 IP 介于代理/路由器/NAT 等之间,则此操作将不起作用...
Assuming you have
ObservableCollection<string> mAccessInfo
somewhere in your code as a publicly visible variable bound to your list, you could place this in the part of the code where you listen for connections:Since it's ObservableCollection, on each new connection that enters the list, the list you're bound to is updated.
Edit: As Cody Gray pointed out, this won't work if you have IP's in between as proxy/router/NAT etc...