iOS Get Link Speed (Router Speed Test)

偶尔善良 提交于 2021-02-07 08:21:31

问题


I want to test speed of connected router(wifi modem) from iOS app.

I've found something here Get link speed programmatically? but could not found sockios.h and ethtool.h

Is it possible to port this code to Objective-C or is there another way?

--

Sorry for the missing info and my poor english.

I want to test link speed (tx rate) between ios device and connected wifi modem.

There was a property named txRate in CWInterface class. I want to get that data in Cocoa Touch.

/*!
* @property
* @abstract Current transmit rate (Mbps) of the CoreWLAN interface. 
* @discussion Dynamically queries the interface for the current transmit rate.
*/
@property(readonly) NSNumber *txRate NS_DEPRECATED_MAC(10_6, 10_7);

回答1:


Finally I've found the solution.

#include <ifaddrs.h>
#include <net/if.h>

+ (double)getRouterLinkSpeed
{
    BOOL   success;
    struct ifaddrs *addrs;
    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc;

    double linkSpeed = 0;

    NSString *name = [[NSString alloc] init];

    success = getifaddrs(&addrs) == 0;
    if (success)
    {
        cursor = addrs;
        while (cursor != NULL)
        {
            name=[NSString stringWithFormat:@"%s",cursor->ifa_name];

            if (cursor->ifa_addr->sa_family == AF_LINK)
            {
                if ([name hasPrefix:@"en"])
                {
                    networkStatisc = (const struct if_data *) cursor->ifa_data;
                    linkSpeed = networkStatisc->ifi_baudrate;
                }
            }
            cursor = cursor->ifa_next;
        }
        freeifaddrs(addrs);
    }

    return linkSpeed;
}



回答2:


You can use NSURLConnection to connect to your test server and download a preset file of something like 1 MB. Use the NSURLConnection delegate -connection:didReceiveData: and -connectionDidFinishLoading:to track the download 'so far' and compute the download speed from that.




回答3:


There is currently no official (or Apple approved) way to get LinkSpeed on iOS. There were loop holes in the past which were closed unfortunately.

The most similar metric you can use to estimate LinkSpeed is measure wifi speed by sending UDP packets on the local network and measuring their sending rate. This is called IP packet sending bit rate and is defined in this ITU standard https://www.itu.int/rec/T-REC-Y.1540/en

The iOS implementation of that wifi speed measurement is in our iOS SDK which you can find here:

https://github.com/speedchecker/speedchecker-sdk-ios

This implementation is the closest you can get to estimate router wifi speed without actually placing any payload files on the router itself.



来源:https://stackoverflow.com/questions/16878982/ios-get-link-speed-router-speed-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!