美文网首页技术类简友广场
Linux 上写C++(9) 判断TCP IP是否开放

Linux 上写C++(9) 判断TCP IP是否开放

作者: 漠北兄弟 | 来源:发表于2020-04-04 10:52 被阅读0次

    //判断TCP/IP是否开放

    int IVR_COMMON::GetIPIsConnect(char *ip,char *port,long timeout)

    {

    int fd;

    struct sockaddr_in addr;

    struct timeval timeo = {3, 0};

    socklen_t len = sizeof(timeo);

    fd = socket(AF_INET, SOCK_STREAM, 0);

    timeo.tv_sec = timeout;

    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, len);

    addr.sin_family = AF_INET;

    addr.sin_addr.s_addr = inet_addr(ip);

    addr.sin_port = htons(atoi(port));

    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {

    if (errno == EINPROGRESS) {

    WriteLog("connect","errno");

    close(fd);

    return -1;

    }

    WriteLog("connect","success");

    close(fd);

    return 0;

    }

    close(fd);

    WriteLog("connect","success");

    return 0;

    }

    相关文章

      网友评论

        本文标题:Linux 上写C++(9) 判断TCP IP是否开放

        本文链接:https://www.haomeiwen.com/subject/ywpdphtx.html