//判断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;
}
网友评论