Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

listylister

macrumors newbie
Original poster
Mar 29, 2008
17
0
Hi There,

I am just starting to learn cocoa and have ordered the recommended book. However it still hasn't arrived - curse amazon.

I was wondering if someone could point me in the correct direction in terms of how to ping a server based on an input field. I have linked the input field already to a var in my class. I also have a text field linked to display the output. Basically all I need is a boolean or time response.

Thanks in advance
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
I don't know if there is a nice easy way to do this in Cocoa itself, but one way to do it is to use NSTask and fire off the shell command.

You could either run the normal ping command (ping 192.168.0.2) and collect and parse the output to give the user detailed information or you could run (ping -o -t 5 192.168.0.5) which will exit at either the first response or after 5 seconds. If the exit code is 0 then the machine was reachable. If the exit code was not zero then it wasn't reachable.

Look here NSTask on CocoaDev for an example. You can call [yourTask terminationStatus] to get the exit code of the ping.

Let me know if you have questions on this.

-numero
 

listylister

macrumors newbie
Original poster
Mar 29, 2008
17
0
First of all thanks for replying. I have tried the NSTask Code but for some reason it doesn't seem to come back with anything. No Errors either. I have tried debugging the code and it just gets stuck on the ping command. My Code is as follows:

Code:
NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/sbin/ping"];

    NSArray *arguments;
    arguments = [NSArray arrayWithObjects: @"-o", @"-t", @"google.com"];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *string;
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    NSLog (@"got\n%@", string);


Thanks for your help
 

listylister

macrumors newbie
Original poster
Mar 29, 2008
17
0
Nevermind I got it working. I forgot to put the 'nil' entry on the end of the array. For some reason that makes a difference :S

Thanks a lot dude
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
I see a little issue with this. You said you got it working so you must have caught this, but for those playing along at home . . .
Change:
Code:
arguments = [NSArray arrayWithObjects: @"-o", @"-t", @"google.com"];
to
Code:
arguments = [NSArray arrayWithObjects: @"-o", @"-t5", @"google.com", nil];

Notice the 5 attached to the -t option. Fill in your own value as you see fit. This is the maximum number of seconds that ping will wait to get a response before aborting. I've also filled in the nil that you already caught.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.