iossocketscfstream

How to connect to public ip using CFStream in iOS


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];
}

Solution

  • Not 100% sure but I'm almost positive CFStream can't connect to the internet. You need a NSStream.