创建p2p连接iOS
好的。 所以我一直在试图将设备p2p与流连接一周。 仍然没有结果,我越来越疯狂和绝望。 请不要将我发送给Ray Wenderlich教程和GCD维基或CFStream指南,因为我已将它冲入漏洞。
所以1)变种在这里我
错误 - 连接到主机。
+ (NSString *)getIPAddress {
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;
}
NSInputStream * inputStream =(__bridge_transfer NSInputStream *)readStream; NSOutputStream * outputStream =(__bridge_transfer NSOutputStream *)writeStream; [inputStream setDelegate:self]; [outputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream打开]; [outputStream打开];
}
NSLog(@“stream event%lu”,streamEvent);
开关(streamEvent){
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[1024];
int len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
NSLog(@"server said: %@", output);
//[self messageReceived:output];
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// [theStream release];
theStream = nil;
break;
default:
NSLog(@"Unknown event");
}
}
变体2与GCD。 1.相同2.将两台设备设置为侦听输入
+(void)listenSocket:(GCDAsyncSocket*)listenSocket
{
listenSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
if (![listenSocket acceptOnPort:80 error:&error])
{
NSLog(@"I goofed: %@", error);
}
}
3)尝试连接手动输入IP
+(void)connectToDeviceWithIp:(NSString*)deviceIp andSend:(HSUserCard*)tempCard andSocket:(GCDAsyncSocket*)tempSocket
{
tempSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![tempSocket connectToHost:deviceIp onPort:80 error:&err])
NSLog(@"I goofed: %@", err);
[tempSocket writeData:[NSKeyedArchiver archivedDataWithRootObject:tempCard] withTimeout:-1 tag:1];
}
4)没有结果 - 我确实在每个委托函数中都放了几个休息时间=没有。
Omg - 我在20分钟内在Android上解决了这个任务! 但在这里它只是疯狂。 尝试了几个网络。 Thrue 3g,家庭无线网络。 请有人帮忙!
从各种尝试中可以看到的问题是,您将套接字存储在本地变量中 - 这意味着一旦方法退出,它将被释放,并且在该套接字上不可能进行通信。
您需要将您的套接字引用存储在strong
属性中。
我创建了一个使用CocoaAsyncSocket的示例应用程序,演示如何监听连接并建立连接,并通过持有套接字的属性跟踪连接状态。
它可以在这里https://github.com/paulw11/SocketDemo
链接地址: http://www.djcxy.com/p/96717.html