[英]How to get peer IP address in iOS app (iOS - non-iOS device peer-to-peer)
I am peering my iOS device with a non-iOS Direct-WiFi enabled device. 我正在将我的iOS设备与未启用iOS Direct-WiFi的设备建立对等关系。 In my iOS application, I like to initiate the connection with the remote server (ie the non-iOS device's server).
在我的iOS应用程序中,我想启动与远程服务器(即非iOS设备的服务器)的连接。 To do this, first I need to bind my read and write streams to the host IP and port as follows:
为此,首先我需要将读写流绑定到主机IP和端口,如下所示:
NSInputStream *inputStream;
NSOutputStream *outputStream;
CFReadStreamRef rdStream;
CFWriteStreamRef wrStream;
NSString *ipAddr = ??? /* not know at design time */
UInt32 portNum = 4210; /* specified at design time */
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)ipAddr, portNum, &rdStream, &wrStream);
inputStream = (__bridge NSInputStream *)rdStream;
outputStream = (__bridge NSOutputStream *)wrStream;
... then for each stream, I set the delegate, schedule in run loop, and open. ...然后,对于每个流,我设置委托,在运行循环中安排时间表,然后打开。 Now they are ready for use.
现在它们可以使用了。 And obviously, in my iOS device, I go to the Settings built-in app and select the WiFi of the server I am peering with so my iOS device and the other device will be on the same network.
显然,在我的iOS设备中,我转到“设置”内置应用程序,然后选择要与之对等的服务器的WiFi,这样我的iOS设备和另一台设备将位于同一网络上。
My problem is the server IP address is not known at my app design time, so I want to figure out this IP programmatically at runtime in my app. 我的问题是服务器IP地址在我的应用程序设计时未知,因此我想在我的应用程序运行时以编程方式找出此IP地址。 Is it possible?
可能吗? If so, please give me an idea.
如果是这样,请给我一个主意。
Thanks 谢谢
This will get you an array of IP's. 这将为您提供一系列IP。 For my purposes the second element was the iP of the host name:
出于我的目的,第二个元素是主机名的IP:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#import <CFNetwork/CFNetwork.h>
#import <netinet/in.h>
#import <netdb.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <net/ethernet.h>
#import <net/if_dl.h>
...
const char* hostnameC = [yourHostName UTF8String];
struct addrinfo hints, *res;
struct sockaddr_in *s4;
struct sockaddr_in6 *s6;
int retval;
char buf[64];
NSMutableArray *result; //the array which will be return
NSMutableArray *result4; //the array of IPv4, to order them at the end
NSString *previousIP = nil;
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = PF_UNSPEC;//AF_INET6;
hints.ai_flags = AI_CANONNAME;
//AI_ADDRCONFIG, AI_ALL, AI_CANONNAME, AI_NUMERICHOST
//AI_NUMERICSERV, AI_PASSIVE, OR AI_V4MAPPED
retval = getaddrinfo(hostnameC, NULL, &hints, &res);
if (retval == 0)
{
if (res->ai_canonname)
{
result = [NSMutableArray arrayWithObject:[NSString stringWithUTF8String:res->ai_canonname]];
}
else
{
//it means the DNS didn't know this host
return;
}
result4= [NSMutableArray array];
while (res) {
switch (res->ai_family){
case AF_INET6:
s6 = (struct sockaddr_in6 *)res->ai_addr;
if(inet_ntop(res->ai_family, (void *)&(s6->sin6_addr), buf, sizeof(buf))
== NULL)
{
NSLog(@"inet_ntop failed for v6!\n");
}
else
{
//surprisingly every address is in double, let's add this test
if (![previousIP isEqualToString:[NSString stringWithUTF8String:buf]]) {
[result addObject:[NSString stringWithUTF8String:buf]];
}
}
break;
case AF_INET:
s4 = (struct sockaddr_in *)res->ai_addr;
if(inet_ntop(res->ai_family, (void *)&(s4->sin_addr), buf, sizeof(buf))
== NULL)
{
NSLog(@"inet_ntop failed for v4!\n");
}
else
{
//surprisingly every address is in double, let's add this test
if (![previousIP isEqualToString:[NSString stringWithUTF8String:buf]]) {
[result4 addObject:[NSString stringWithUTF8String:buf]];
}
}
break;
default:
NSLog(@"Neither IPv4 nor IPv6!");
}
//surprisingly every address is in double, let's add this test
previousIP = [NSString stringWithUTF8String:buf];
res = res->ai_next;
}
}else{
NSLog(@"no IP found");
return;
}
... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.