iosxcode4.5cfstream

CFStream connection cannot connect while using public ip


I am a newbie to iOS and Objective c.... In my app i am establishing a socket connection to my server using the CFStream.. the problem is when i run my program using the local ip (192.168.10.246) the connection is established, but when i replace it with a public ip the view rather freezes and give the following error event..

I am currently running on the simulator...

2013-06-27 12:50:41.778 BarcodeGenerator[446:c07] Start StreamEvent 8
2013-06-27 12:50:41.778 BarcodeGenerator[446:c07] Can not connect to the host!
2013-06-27 12:50:41.779 BarcodeGenerator[446:c07] Start StreamEvent 8
2013-06-27 12:50:41.780 BarcodeGenerator[446:c07] Can not connect to the host!

And my code is..

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ip, 54000, &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];

Do i need to add some extra piece of code for public ip... or is it some kind of a firewall problem in network connection or a setting in Xcode??? Please Help!!!!

Edit:

I have attached the wireshark logs: My destination public ip is 221.135.139.46....

No.     Time           Source                Destination           Protocol Length Info
     83 2.426998000    192.168.43.131        221.135.139.46        TCP      78     50853 > 54000 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=16 TSval=61451776 TSecr=0 SACK_PERM=1

Frame 83: 78 bytes on wire (624 bits), 78 bytes captured (624 bits) on interface 0
Ethernet II, Src: Apple_13:f4:a1 (00:23:12:13:f4:a1), Dst: SamsungE_75:d9:c7 (94:63:d1:75:d9:c7)
Internet Protocol Version 4, Src: 192.168.43.131 (192.168.43.131), Dst: 221.135.139.46 (221.135.139.46)
Transmission Control Protocol, Src Port: 50853 (50853), Dst Port: 54000 (54000), Seq: 0, Len: 0

Solution

  • Probably a firewall related error. Do you forward your public ip to a host? Have you opened the port in the firewall. Test to connect to an known to work public ip before trying to connect to your server.

    You need:

    Depending on the level of your router this terminology will be Port Forwarding, Virtual IPs, or Static NAT Translation (there are a few others but you get the idea).

    If your android version is working, there might be something else causing the problem. Ray Wenderlich has a good example of how to implement a stream app. You can analyze what is actually being passed on the network using wireshark to analyze further and also look into the router logs. Im unsure since you say android works with the public ip