0

I'm using CocoaAsyncTask and trying to connect to the localhost. When I run it from the simulator, it works (I'm checking it with the provided echo server) but when I run it on the actual device, nothing happens.. the code is:

if(![socket connectToHost:@"localhost" onPort:8080 error:nil]) {
    NSLog(@"Error..");
}
-(void)socket:(GCDAsyncSocket *)socket didConnectToHost:(NSString *)host: port:(uint16_t)port {
    NSLog(@"Connected");
}

Any ideas? Thanks!

La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • What service are you expecting on the iOS device itself that is serving on port 8080? – Till Apr 26 '12 at 23:20
  • The port itself doesn't matter.. I tried using 4444 as well. at the moment just sending string. it works from the simulator. not from the device – La bla bla Apr 26 '12 at 23:23
  • So you are running both, a server and the client on a non jailbroken device, correct? – Till Apr 26 '12 at 23:36
  • No, the server is at the Macbook (or PC, tried both) and the client is an iphone – La bla bla Apr 27 '12 at 00:01
  • Well, in that case "localhost" won't do. You need to provide the IP-address of your MacBook (or PC). – Till Apr 27 '12 at 00:15

1 Answers1

2

You are providing the address as being "localhost" whilst you attempt to connect to a server that is not locally installed (local = on the exact same host and interface). See the wikipedia entry on "localhost".

It works from the simulator as that one uses your Mac's networking interface, hence "localhost" refers to the machine you run the server from as well as the client.

You will need to provide the local IP address of your hosting machine (e.g. your Mac). Usually that is something like 192.168.1.100 or 192.168.0.100 - but to be more precise on that stuff;

192.168.xxx.xxx = class C non-routable addresses

10.xxx.xxx.xxx = class A non-routable addresses

Whereas xxx stands for any value between 0 and 255.

To find out about your local IP-address, use the System Preferences -> Network Panel. Check the value below Status for "...the IP address xxx.xxx.xxx.xxx"

enter image description here

Till
  • 27,559
  • 13
  • 88
  • 122
  • I tried. didn't work either.. I checked my ip from www.whatismyip.com to verify – La bla bla Apr 27 '12 at 00:23
  • You tried a non-local address but that is not what your Mac is reachable on if you are behind a router with a firewall. Either open that port on your firewall or use a local address. – Till Apr 27 '12 at 00:28
  • i'll give it a shot tommorow. If I want to use the non local address (to test from outside the local network) i need to test with the address obtained from www.whatismyip.com (or ipconfig..) and open the port in the router? that's it? – La bla bla Apr 27 '12 at 00:30
  • First try it using local routing (local IP address). Then, if that works as intended, go ahead and make sure the selected port is reachable remotely. For more on that issue, see the documentation of your router/firewall. – Till Apr 27 '12 at 00:32
  • Thanks. I will try it tommorow! Thanks a lot for you help – La bla bla Apr 27 '12 at 00:34
  • Note, your iOS device has to be connected to the very same network for attempting a local routing approach. That is, it has to be connected via WiFi to the same router/network your server (mac/pc) is connected to. – Till Apr 27 '12 at 00:35