通过管道使用 curl 来测试网络是否可用

测试当前网络是否可用的命令有 Ping 和 curl,由于在公司内网中使用 ping 会存在失败的情况,所以推荐使用 curl 命令来进行测试。

bool AccessNetworkTest()
{
    FILE* fp = _wpopen(L"curl www.baidu.com", L"r");
    wchar_t buff[1024] = { 0 };

    while (fgetws(buff, 1024, fp) != NULL)
    {}

    int ret = _pclose(fp);

    printf("ret = %d\n", ret);

    if (ret == 0)
    {
        printf("Ping Successful\n");
        return true;
    }
    else
    {
        printf("Ping Failed\n");
        return false;
    }
}

_wpopen 用法参考

posted @ 2023-05-06 17:21  Jeffxue  阅读(45)  评论(0编辑  收藏  举报