免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1592 | 回复: 0
打印 上一主题 下一主题

急救ping网络的问题,附有完整代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-09 11:18 |只看该作者 |倒序浏览
本帖最后由 linuxkiller7967 于 2013-01-09 11:18 编辑

问题描述:当使用ls -l命令,也就是用fpout = popen("ls -l /Customer","r")这个语句的时候,程序顺利运行。ping.txt里面也存储了所有完成的流信息。
但是当fpout = popen("ls -l /Customer","r")换成fpout = popen("ping -c3 192.168.0.1","r")时,用ps查看这个进程始终不消失,卡死,整个程序也运行不完,ping.txt里面也没有数据。
跪求指点啊。为什么同样的程序,只是采用了两个不同的命令而已,为什么会出现差别巨大的结果呢?


BOOL Network_Detect(void)
{
        printf("\n FUNC=%s,LINE=%d\n",__func__,__LINE__);
        BOOL lanStatus = FALSE;
        FILE* fpout = NULL;
        FILE* fpin = NULL;
        FILE* pFile = NULL;
        int nCount_fread;
        int nCount_write;
        char buf[20],temp[1024];
        memset(buf,'\0',sizeof(buf));
        memset(temp,'\0',sizeof(temp));
        if(TRUE == Network_Detect_Eth0_NetworkCable_State())  //check wheather  is connected with NetworkCable
        {
                if( (fpout = popen("ls -l /Customer","r")) == NULL)
                        printf("pclose error!\n");
               
                if((fpin= fopen("/Customer/ping.txt","w+"))== NULL) //check wheather creat new file success
                {
                        printf(" fopen creat file failed!\n");               
                }
                nCount_fread = fread(temp,sizeof(char),sizeof(temp),fpout);
                printf("temp is equal to =%s\n",temp);
                if(nCount_fread != sizeof(temp))
                {
                        printf(" fread %d data from stream\n",nCount_fread);
                        printf(" fread file failed/not complete!\n");
                }
                nCount_write = fwrite(temp,1,sizeof(temp),fpin);
                if( nCount_write != sizeof(temp))
                {
                        printf(" fwrite %d data to file\n",nCount_write);
                        printf("fwrite data to file failed or not completed!\n");
                }
                if((pFile = popen("cat /Customer/ping.txt","r")) == NULL)
                {
                        printf(" Popen cat data failed!\n");
                }
                if(pclose(fpout) == -1)
                        printf("pclose error!\n");
                fclose(fpin);
                while(fgets(buf,20,pFile))
                {
                        printf(" fgets do!\n");
                        if(strstr(buf,"Ping"))
                        {
                                printf(" All packets Loss \n");
                                lanStatus = 0;
                                printf(" lanStatus is = %d\n",lanStatus);
                                break;
                        }
                        else if(strstr(buf,"boot.ini"))
                        {
                                printf(" No packets Loss\n");
                                lanStatus = 1;
                                printf(" lanStatus is = %d\n",lanStatus);
                                break;
                        }
                }
                if(pclose(pFile) == -1)
                        printf("pclose error too!\n");
        }
        else
        {
                printf(" not connected with NetworkCable!\n");
                printf("LanStatus is = %d\n",lanStatus);
                lanStatus = FALSE;
        }
        return TRUE;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP