int check_net(char *ip,char *port )
{
int fd;
int in_len=0;
struct sockaddr_in servaddr;
//char buf[128];
in_len = sizeof(struct sockaddr_in);
fd = socket(AF_INET,SOCK_STREAM,0);
if(fd < 0)
{
perror("socket");
return -1;
}
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(port));
servaddr.sin_addr.s_addr = inet_addr(ip);
memset(servaddr.sin_zero,0,sizeof(servaddr.sin_zero));
printf("ip = %s",ip);
printf("port = %s",port);
if(connect(fd,(struct sockaddr* )&servaddr,in_len) < 0 )
{
printf("not connect to internet!\n ");
close(fd);
return 0;
}
else
{
printf("=====connect ok!=====\n");
close(fd);
return 1;
}
}
int check_net(char *ip,char *port )
传入要ping的服务器ip port,
返回值为1,连接正常。
返回0,无连接。
阅读(794) | 评论(0) | 转发(0) |