开发者

How to calculate response time (ping) from iphone programmatically

I want to check whether servers are active or not from the iphone application programmatically.

How do I calculate the time for getting response from the server?

I have heard a开发者_JAVA技巧bout ping, what to use it?


You could try to connect to your server (using for example Reachability by Apple http://developer.apple.com/iphone/library/samplecode/Reachability/index.html) and use an NSTimer to time the response. But that won't be as precise as a ping command


When you want to get any response from server , first check if any network connection available or not by implementing Reachability as told by Thomas.

Now if you have active network connection then you can request the server and catch the response as described below.

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

        NSHTTPURLResponse *httpResponse;
        httpResponse = (NSHTTPURLResponse *)response;
        int statusCode = [httpResponse statusCode];
        NSString *serverResponse = [NSString stringWithFormat:@"%d", statusCode];
        NSLog ( @ "status code =% d" , statusCode);
        if(statusCode!=200){
            UIAlertView * Alert = [[ UIAlertView alloc ] initWithTitle : @"Problem"
                                                               Message : serverResponse
                                                              delegate : nil 
                                                     cancelButtonTitle : @ " OK "
                                                     otherButtonTitles : nil ];
            [Alert Show ];
            [Alert Release ];

        }
}

You can refer this for List of HTTP Status Code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜