如何获取iOS中用于连接的确切IP地址? [英] How can I get the exactly ip address which used to connect in iOS?

查看:178
本文介绍了如何获取iOS中用于连接的确切IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用域名创建请求,例如 http://www.google.com 。但是,如何获得框架用于连接服务器的确切IP地址?

I created a request using a domain name, e.g., http://www.google.com. But how could I get the exactly ip address which the framework used to connect to the server?

我知道 gethostbyname 方法或 nslookup 方法可以给我们地址,但是由于ip地址是动态分配的,因此也许我要求的IP地址与返回的这些方法不同。

I knew that the gethostbyname method or the nslookup method could give us the address, but since the ip address is dynamic allocated, maybe the ip address I request that time is different with these methods returned.

那么我有办法获取真正的请求IP地址吗? (我想以编程方式获取ip,而不是使用tcpdump等。)

So is there a way for me to get the real requested ip address? (I wanted to get the ip programmatically, rather than using tcpdump, etc.)

推荐答案

当您说框架使用了哪个在这里,我假设您的意思是 NSURLSession NSURLConnection UIWebView WKWebView 。每种情况都稍有不同,但是在所有情况下,答案都是不可能直接(但请参阅下面;可以间接地)。您无权访问其中任何一个使用的基础套接字。它们都使用连接池,即使您可以获得套接字,这也会使事情稍微复杂一些。对于 UIWebView WKWebView 而言,对 www.google.com的单个请求可能会生成几个独立的连接,每个连接都可能与其他IP地址进行交互。

When you say "which the framework used" here, I assume you mean NSURLSession, NSURLConnection, UIWebView, or WKWebView. Each of those is a slightly different situation, but in all of them, the answer is that it's not possible directly (but see below; it's possible indirectly). You have no access to the underlying sockets that any of them use. All of them use connection pooling, which complicates things slightly even if you could get "the socket." And in the case of UIWebView and WKWebView, a single request to www.google.com may generate several independent connections, each of which could potentially interact with a different IP address.

(我对您的应用颇为着迷'正在尝试做。由于负载平衡,单个IP地址并不意味着单个服务器,因此IP地址仅比CNAME更具识别性。混合使用反向代理....)

(I'm a bit fascinated about what you're trying to do. Due to load balancing, a single IP address doesn't mean a single server, so IP addresses are only marginally more identifying than CNAMEs. Mixing in reverse proxies....)

如果需要这种访问方式,则必须自己管理套接字。除了 WKWebView 之外,所有其他系统通常都可以做到这一点。我假设您已经知道(或可以轻松学习)如何使用 CFSocket CFHTTPMessage 《 CFNetwork编程指南》 对此进行了广泛讨论。一个>。如果创建了套接字,则可以使用 CFSocketCopyPeerAddress 检查您真正连接的主机。

If you need this kind of access, you have to manage the socket yourself. That's generally possible with all of the systems except WKWebView. I'll assume that you know already (or can easily study) how to create a socket and perform HTTP using CFSocket and CFHTTPMessage. This is extensively covered in the CFNetwork Programming Guide. If you've created the socket, you can use CFSocketCopyPeerAddress to check what host you really connected to. That's the piece you wanted.

鉴于您能够创建这种套接字并自行管理,您可以将其挂接到主要的URL加载器中(<$ c除外) $ c> WKWebView ),使用 NSURLProtocol 。有关快速介绍和一些示例代码,请参见针对UIWebView(和NSURLProtocol)的脱机脱机缓存。您只需要接受请求,并使用 CFSocket 进行设置即可,从而有机会查看所连接的确切端口。

Given you are able to create this kind of socket and manage it yourself, you can hook that into the major URL loaders (except WKWebView) using an NSURLProtocol. See Drop-in Offline Caching for UIWebView (and NSURLProtocol) for a quick introduction and some sample code. You just need to take the request and make it yourself with CFSocket, giving you the chance to see the exact port you're connected to.

这篇关于如何获取iOS中用于连接的确切IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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