0

I am a beginner in iOS and objective C

I am using the following code in order to connect with the server... When i am connecting locally using the ip 192.168.10.246 the code works fine and it gets connected but when i try to use a public ip instead the view just freezes and doesn't connect to the server ....

I am using the tutorial given here by ray wenderlich

And my code is:

NSString *pinno = @"test1234";
NSString *ip = @"publicip";

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ip, 1234, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

NSData *data = [[NSData alloc] initWithData:[pinno dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
[outputStream close];
}
4

1 回答 1

0

不是 100% 肯定,但我几乎肯定 CFStream 无法连接到互联网。你需要一个 NSStream。

于 2016-06-06T18:21:30.957 回答