views:

113

answers:

2

Hi guys

My program consists of 2 parts - A server socket running on a different thread, and a client to test the server. I'm 99.9% sure that the server is correctly written (because I have tested it with a client socket), and all it does is returns "hello world" as a response. The server sits on port 3490.

Now what I tried to do is to call the server in the following way:

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"http://localhost:3490"]];
    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(msg);

But I get no response. I suspect that the problem is with the address http://localhost:3490

This problem appears on the iphone an on mac.

Any ideas?

Thanks

Alex

I have this problem on the iphone and on mac.

+2  A: 

First, you almost certainly want to be using +[NSURL URLWithString:] instead of +[NSURL fileURLWithPath:].

Second, since -[NSData initWithContentsOfURL:] hides so much of what's going on to make the connection, you might consider switching to an NSURLConnection you manage yourself, at least temporarily. The delegate methods it uses will give you a lot more visibility to / control over the various stages of the process. (As well as not blocking the thread on which you're running this code.)

Sixten Otto
Thanks man, you helped me out a lot!!!!
Alex1987
A: 

It is possible that localhost resolves to an IPv6 address. Try using http://127.0.0.1:3490/ instead first before turning to NSURLConnection.

St3fan