user-0 提问: 【求教】怎么用ping判断网络通不通

回复:
user: user-1
content: 仅供参考：#include <windows.h> #include <stdio.h> #include <string.h> char YN(int k) {     FILE *f;     char fn[40];     char ln[80];     char yn;     int n;      yn='N';     sprintf(fn,"d:\\ping%d.txt",k);     f=fopen(fn,"r");     if (NULL!=f) {         n=0;         while (1) {             if (NULL==fgets(ln,80,f)) break;//             if (strstr(ln,"ms ")) {                 yn='Y';                 break;//             }             n++;             if (n>=4) break;//         }         fclose(f);     }     return yn; } void main(int argc,char **argv) {     char cmdstr[256];     int i;     int IP[3];     char c;      if (argc<2) {     USAGE:         printf("Usage example:\n    %s 192.168.60.\nto test 192.168.60.1-254\n",argv[0]);         return;     }     if (4==sscanf(argv[1],"%d.%d.%d%c",&IP[0],&IP[1],&IP[2],&c)) {         if (0<=IP[0] && IP[0]<=255          && 0<=IP[1] && IP[1]<=255          && 0<=IP[2] && IP[2]<=255          && '.'==c) {             for (i=1;i<255;i++) {                 sprintf(cmdstr,"cmd /c ping %s%d -n 1 -w 1000 >d:\\ping%d.txt",argv[1],i,i);                 WinExec(cmdstr,SW_HIDE);             }             Sleep(3000);             for (i=1;i<255;i++) {                 printf("%c %s%d\n",YN(i),argv[1],i);             }             Sleep(3000);             WinExec("cmd /c del /q d:\\ping*.txt",SW_HIDE);         } else goto USAGE;     } else goto USAGE; }

user: user-2
content: 在Linux下直接ping 加IP地址就可以了。需要编程吗？如果编程建议使用libpcap库，自己封装ping协议包；然后通过库函数通过网卡发出去。 另外，有些网管或服务器是禁ping的，所以ping也不能完全作为网络通的有理证据。

user: user-3
content: 稍微修改了下  #include <stdio.h> #include <string.h>  #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h>   static struct addrinfo *get_host_addrinfo(const char *host, int port) { 	struct addrinfo hints, *pAI; 	char service[16];  	memset(&hints, 0, sizeof(hints)); 	hints.ai_family = AF_UNSPEC;	// don't care ipv4 or ipv6 	hints.ai_socktype = SOCK_STREAM; 	hints.ai_protocol = IPPROTO_TCP;  	sprintf(service, "%d", port); 	if (getaddrinfo(host, service, &hints, &pAI)) 		pAI = NULL;  	return pAI; }  static int connect_to_addr(struct addrinfo *pAI, int *pSock) { 	struct addrinfo *p;  	for (p = pAI; p != NULL; p = p->ai_next) 	{ 		*pSock = socket(p->ai_family, p->ai_socktype, p->ai_protocol); 		if (*pSock != -1) 		{ 			if (connect(*pSock, p->ai_addr, p->ai_addrlen)) 				close(*pSock); 			else 				break; 		} 	}  	return p == NULL; }  static int connect_to_host(const char *host, int port) { 	struct addrinfo *pAI; 	int sock = -1;  	if ((pAI = get_host_addrinfo(host, port))) 	{ 		if (connect_to_addr(pAI, &sock)) 			sock = -1; 		freeaddrinfo(pAI); 	} 	 	return sock; }  static int check_connection(const char *host) { 	if (!host) 		host = "www.baidu.com";  	int sock = connect_to_host(host, 80); 	if (sock != -1) 		close(sock);  	return sock != -1; }   int main() { 	int status = check_connection(NULL); 	if (status) 		printf("connected\n"); 	else 		printf("not connected\n"); }

user: user-3
content: 仅供参考，以下代码只是尝试连接到www.baidu.com，并没有真的ping  #include <stdio.h> #include <string.h>  #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h>   static struct addrinfo *get_host_addrinfo(const char *host, int port) { 	struct addrinfo hints, *pAI; 	char service[16]; 	int rv;  	memset(&hints, 0, sizeof(hints)); 	hints.ai_family = AF_UNSPEC;	// don't care ipv4 or ipv6 	hints.ai_socktype = SOCK_STREAM; 	hints.ai_protocol = IPPROTO_TCP;  	sprintf(service, "%d", port); 	if ((rv = getaddrinfo(host, service, &hints, &pAI))) 		pAI = NULL;  	return pAI; }  static int connect_to_addr(struct addrinfo *pAI, int *pSock) { 	struct addrinfo *p;  	for (p = pAI; p != NULL; p = p->ai_next) 	{ 		*pSock = socket(p->ai_family, p->ai_socktype, p->ai_protocol); 		if (*pSock != -1) 		{ 			if (connect(*pSock, p->ai_addr, p->ai_addrlen)) 				close(*pSock); 			else 				break; 		} 	}  	return p == NULL; }  static int connect_to_host(const char *host, int port) { 	struct addrinfo *pAI; 	int sock = -1;  	if ((pAI = get_host_addrinfo(host, port))) 		if (connect_to_addr(pAI, &sock)) 			sock = -1;  	freeaddrinfo(pAI); 	return sock; }  static int check_connection(const char *host) { 	if (!host) 		host = "www.baidu.com";  	int sock = connect_to_host(host, 80); 	if (sock != -1) 		close(sock);  	return sock != -1; }    int main() { 	int status = check_connection(NULL); 	if (status) 		printf("connected\n"); 	else 		printf("not connected\n"); }

user: user-0
content: 引用 5 楼 vnvlyp 的回复:看你什么平台，Windows的话可以使用InternetCheckConnection if(InternetCheckConnection(L"http://www.baidu.com", FLAG_ICC_FORCE_CONNECTION, 0)) {     // 网络正常 }  或者你自己弄个socket，然后尝试连接一个服务器。  好像是linux的，因为我师父让我找的是linux socket ping

user: user-3
content: 看你什么平台，Windows的话可以使用InternetCheckConnection if(InternetCheckConnection(L"http://www.baidu.com", FLAG_ICC_FORCE_CONNECTION, 0)) {     // 网络正常 }  或者你自己弄个socket，然后尝试连接一个服务器。

user: user-0
content: int CO_GetNetCardInfo(char *pNetCard, char *pBuf){          int ret = 0;          if((pNetCard == NULL) || (pBuf == NULL)){         return -1;     }     S_NetCardInfo* pCardInfo = (S_NetCardInfo*) pBuf;                  if(get_ipaddr(pNetCard, pCardInfo->ipAddr)){         ret++;             }      if(get_netmask(pNetCard, pCardInfo->maskAddr)){         ret++;     }      if(get_mac(pNetCard, pCardInfo->macAddr)){         ret++;     }      if(get_broadcast(pNetCard,pCardInfo->boardCast)){         ret++;     }      dbgPrintf(LLEV_DBG, "pCardInfo->ipAddr= %s\n",pCardInfo->ipAddr);     dbgPrintf(LLEV_DBG, "pCardInfo->maskAddr= %s\n",pCardInfo->maskAddr);     dbgPrintf(LLEV_DBG, "pCardInfo->macAddr= %s\n",pCardInfo->macAddr);     dbgPrintf(LLEV_DBG, "pCardInfo->boardCast= %s\n",pCardInfo->boardCast);          return (ret == 0 ? 0 : -1); }    代码如上

user: user-0
content: 引用 1 楼 cyfcsd 的回复:如果是windows就进入dos，如果是linux就直接在命令行中 输入ping xxx.xxx.xxx.xxx回车就可以了，xxx的地方就是你要连接的ip地址，如果判断是否连接到互联网，可以ping www.baidu.com 好吧是我没说清，我意思是在代码中ping一下之后判断通不通，通的话显示Ip地址什么的，不通再返回别的地方。我现在具体的情况您可以看一下3楼

user: user-0
content: 手头问题现在的情况是断网的情况下ifconfig还会显示ip地址等，在开发的APP中也会显示ip，通过重启网卡会超时，想请教一下ping的方法

user: user-4
content: 如果是windows就进入dos，如果是linux就直接在命令行中 输入ping xxx.xxx.xxx.xxx回车就可以了，xxx的地方就是你要连接的ip地址，如果判断是否连接到互联网，可以ping www.baidu.com