Ping Server

Discussion in 'Mac Programming' started by listylister, Jul 11, 2008.

  1. listylister macrumors newbie

    Joined:
    Mar 29, 2008
    #1
    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
     
  2. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #2
    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
     
  3. listylister thread starter macrumors newbie

    Joined:
    Mar 29, 2008
    #3
    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
     
  4. listylister thread starter macrumors newbie

    Joined:
    Mar 29, 2008
    #4
    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
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
  6. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #6
    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.
     
  7. listylister thread starter macrumors newbie

    Joined:
    Mar 29, 2008
    #7
    Yeah thanks both, fixed both of those issues.
     

Share This Page