【发布时间】:2015-01-07 23:35:56
【问题描述】:
我有一个硬件设备采集单元。该设备本身充当wifi路由器,它将发送数据。我想从 iPhone/iPad 连接它。
我听说使用 IP 地址和端口号我们可以使用套接字编程连接到 wifi 路由器。但是我不知道这个socket程序使用IP地址和端口号连接到wifi。
为此,我尝试使用以下代码获取连接的 wifi 路由器的 IP 地址。
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
如果可以使用 ip 地址和端口号连接到 wifi 路由器,请您帮我一些代码或想法。
请帮帮我..
谢谢
【问题讨论】:
标签: ios iphone ipad sockets wifi