0

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
Adolf Dsilva
  • 13,092
  • 8
  • 34
  • 45

2 Answers2

1

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

David Karlsson
  • 9,396
  • 9
  • 58
  • 103
  • i just checked my firewall settings... my firewall is turned off... can you tell me any known public ip for which i can test my connection – Adolf Dsilva Jun 27 '13 at 07:48
  • You need to forward the connection, what router/firewall are you using? – David Karlsson Jun 27 '13 at 07:52
  • I was using my office net... so i thought it might be protected by some firewall not allowing to establish any public connections... But later i tried by my personal data card net... but still no luck... i have implement a same replica in android using socket connection..its working fine... No idea about iOS – Adolf Dsilva Jun 27 '13 at 07:58
  • You need to forward the public ip through the router to the host you want to connect to – David Karlsson Jun 27 '13 at 08:00
  • Wireshark logs? You should update your question with the info bout the workng android version and which tutorial you have been using – David Karlsson Jun 27 '13 at 09:49
  • Did you get a response from the server at all? Is your android version also on port 54000 – David Karlsson Jun 29 '13 at 22:35
0

This guy has another approach of handling the write to the NSOutputStream His problem sounds similar to yours. The problem is that his app stalls and nothing happens on the line [outputStream write:[data bytes] maxLength:[data length]];

Community
  • 1
  • 1
David Karlsson
  • 9,396
  • 9
  • 58
  • 103